diff options
author | ᴜɴᴋɴᴡᴏɴ <u@gogs.io> | 2020-02-29 22:24:20 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-29 22:24:20 +0800 |
commit | 8796df8218aa306f1431c17233fbcc3e0811cc4e (patch) | |
tree | 07ba5f0e62ed68064143f029217c82c80f9b652c /internal/conf/computed_test.go | |
parent | 17ae0ed3eef54d96bd179ff7fec0540cf3024748 (diff) |
conf: add unit tests (#5954)
* conf: add tests for utils.go
* conf: add tests for static.go
* mock os/exec
* Run tests on Windows
* appveyor: fix gcc not found
* computed: add unit tests
* log: add unit tests
* log: fix tests on Windows
* conf: add some tests
* Finish adding tests
* Cover more cases
* Add tests for testutil
* Add more tests
Diffstat (limited to 'internal/conf/computed_test.go')
-rw-r--r-- | internal/conf/computed_test.go | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/internal/conf/computed_test.go b/internal/conf/computed_test.go new file mode 100644 index 00000000..908b7679 --- /dev/null +++ b/internal/conf/computed_test.go @@ -0,0 +1,126 @@ +// Copyright 2020 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package conf + +import ( + "fmt" + "os" + "testing" + + "github.com/stretchr/testify/assert" + + "gogs.io/gogs/internal/testutil" +) + +func TestIsProdMode(t *testing.T) { + before := App.RunMode + defer func() { + App.RunMode = before + }() + + tests := []struct { + mode string + want bool + }{ + {mode: "dev", want: false}, + {mode: "test", want: false}, + + {mode: "prod", want: true}, + {mode: "Prod", want: true}, + {mode: "PROD", want: true}, + } + for _, test := range tests { + t.Run("", func(t *testing.T) { + App.RunMode = test.mode + assert.Equal(t, test.want, IsProdMode()) + }) + } +} + +func TestWorkDirHelper(t *testing.T) { + if !testutil.WantHelperProcess() { + return + } + + fmt.Fprintln(os.Stdout, WorkDir()) +} + +func TestWorkDir(t *testing.T) { + tests := []struct { + env string + want string + }{ + {env: "GOGS_WORK_DIR=/tmp", want: "/tmp"}, + {env: "", want: WorkDir()}, + } + for _, test := range tests { + t.Run("", func(t *testing.T) { + out, err := testutil.Exec("TestWorkDirHelper", test.env) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, test.want, out) + }) + } +} + +func TestCustomDirHelper(t *testing.T) { + if !testutil.WantHelperProcess() { + return + } + + fmt.Fprintln(os.Stdout, CustomDir()) +} + +func TestCustomDir(t *testing.T) { + tests := []struct { + env string + want string + }{ + {env: "GOGS_CUSTOM=/tmp", want: "/tmp"}, + {env: "", want: CustomDir()}, + } + for _, test := range tests { + t.Run("", func(t *testing.T) { + out, err := testutil.Exec("TestCustomDirHelper", test.env) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, test.want, out) + }) + } +} + +func TestHomeDirHelper(t *testing.T) { + if !testutil.WantHelperProcess() { + return + } + + fmt.Fprintln(os.Stdout, HomeDir()) +} + +func TestHomeDir(t *testing.T) { + tests := []struct { + envs []string + want string + }{ + {envs: []string{"HOME=/tmp"}, want: "/tmp"}, + {envs: []string{`USERPROFILE=C:\Users\Joe`}, want: `C:\Users\Joe`}, + {envs: []string{`HOMEDRIVE=C:`, `HOMEPATH=\Users\Joe`}, want: `C:\Users\Joe`}, + {envs: nil, want: ""}, + } + for _, test := range tests { + t.Run("", func(t *testing.T) { + out, err := testutil.Exec("TestHomeDirHelper", test.envs...) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, test.want, out) + }) + } +} |