aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2018-05-17 06:12:59 +0700
committerShulhan <ms@kilabit.info>2018-05-17 06:12:59 +0700
commitefa4f97627b70ccba2f98574090abd751bc11d83 (patch)
tree394041ae3a0cb212c7d57f999bc785c212be7914
parentb6e72ab41aa1ab207cb255d38158271634e558ae (diff)
downloadbeku-efa4f97627b70ccba2f98574090abd751bc11d83.tar.xz
[test] Move test initialization and common test functions to beku_test
-rw-r--r--beku_test.go107
-rw-r--r--package_git_test.go196
-rw-r--r--package_test.go98
3 files changed, 205 insertions, 196 deletions
diff --git a/beku_test.go b/beku_test.go
new file mode 100644
index 0000000..06fa37e
--- /dev/null
+++ b/beku_test.go
@@ -0,0 +1,107 @@
+package beku
+
+import (
+ "go/build"
+ "io"
+ "io/ioutil"
+ "log"
+ "os"
+ "testing"
+)
+
+const (
+ testGitRepo = "github.com/shuLhan/beku_test"
+)
+
+var (
+ testEnv *Env
+ gitCurPkg *Package
+ gitNewPkg *Package
+ testStdout *os.File
+ testStderr *os.File
+)
+
+func testInitOutput() (err error) {
+ testStdout, err = ioutil.TempFile("", "")
+ if err != nil {
+ return
+ }
+
+ testStderr, err = ioutil.TempFile("", "")
+ if err != nil {
+ return
+ }
+
+ defStdout = testStdout
+ defStderr = testStderr
+
+ return
+}
+
+func testGetOutput(t *testing.T) (stdout, stderr string) {
+ bout, err := ioutil.ReadAll(defStdout)
+ if err != nil {
+ t.Fatal(err)
+ }
+ berr, err := ioutil.ReadAll(testStderr)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ stdout = string(bout)
+ stderr = string(berr)
+
+ return
+}
+
+func testResetOutput(t *testing.T, truncate bool) {
+ _, err := testStdout.Seek(0, io.SeekStart)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ _, err = testStderr.Seek(0, io.SeekStart)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if truncate {
+ testStdout.Truncate(0)
+ testStderr.Truncate(0)
+ }
+}
+
+func TestMain(t *testing.M) {
+ orgGOPATH := build.Default.GOPATH
+
+ testGOPATH, err := os.Getwd()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ testGOPATH += "/testdata"
+ build.Default.GOPATH = testGOPATH
+
+ defer func() {
+ build.Default.GOPATH = orgGOPATH
+ }()
+
+ err = testInitOutput()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ testEnv, err = NewEnvironment()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ gitCurPkg = NewPackage(testGitRepo, testGitRepo, VCSModeGit)
+ gitNewPkg = NewPackage(testGitRepo, testGitRepo, VCSModeGit)
+
+ log.Printf("test env : %+v\n", *testEnv)
+ log.Printf("gitCurPkg: %+v\n", *gitCurPkg)
+ log.Printf("gitNewPkg: %+v\n", *gitNewPkg)
+
+ os.Exit(t.Run())
+}
diff --git a/package_git_test.go b/package_git_test.go
index 193cffb..d9394fd 100644
--- a/package_git_test.go
+++ b/package_git_test.go
@@ -1,77 +1,11 @@
package beku
import (
- "go/build"
- "io"
- "io/ioutil"
- "os"
"testing"
"github.com/shuLhan/share/lib/test"
)
-const (
- testGitRepo = "github.com/shuLhan/beku_test"
-)
-
-var (
- testEnv *Env
- gitCurPkg *Package
- gitNewPkg *Package
- testStdout *os.File
- testStderr *os.File
-)
-
-func testInitOutput() (err error) {
- testStdout, err = ioutil.TempFile("", "")
- if err != nil {
- return
- }
-
- testStderr, err = ioutil.TempFile("", "")
- if err != nil {
- return
- }
-
- defStdout = testStdout
- defStderr = testStderr
-
- return
-}
-
-func testGetOutput(t *testing.T) (stdout, stderr string) {
- bout, err := ioutil.ReadAll(defStdout)
- if err != nil {
- t.Fatal(err)
- }
- berr, err := ioutil.ReadAll(testStderr)
- if err != nil {
- t.Fatal(err)
- }
-
- stdout = string(bout)
- stderr = string(berr)
-
- return
-}
-
-func testResetOutput(t *testing.T, truncate bool) {
- _, err := testStdout.Seek(0, io.SeekStart)
- if err != nil {
- t.Fatal(err)
- }
-
- _, err = testStderr.Seek(0, io.SeekStart)
- if err != nil {
- t.Fatal(err)
- }
-
- if truncate {
- testStdout.Truncate(0)
- testStderr.Truncate(0)
- }
-}
-
func testGitCompareVersion(t *testing.T) {
cases := []struct {
desc string
@@ -283,139 +217,9 @@ func testGitScanDeps(t *testing.T) {
}
}
-func testAddDep(t *testing.T) {
- cases := []struct {
- desc string
- envPkgs []*Package
- importPath string
- exp bool
- expDeps []string
- expDepsMissing []string
- expPkgsMissing []string
- }{{
- desc: "Is the same path as package",
- importPath: testGitRepo,
- }, {
- desc: "Is vendor package",
- importPath: "vendor/github.com/shuLhan/beku",
- }, {
- desc: "Is standard package",
- importPath: "os/exec",
- }, {
- desc: "Is exist on environment",
- envPkgs: []*Package{{
- ImportPath: "github.com/shuLhan/beku",
- }, {
- ImportPath: "github.com/shuLhan/share",
- }},
- importPath: "github.com/shuLhan/share/lib/test",
- exp: true,
- expDeps: []string{
- "github.com/shuLhan/share",
- },
- }, {
- desc: "Is exist on environment (again)",
- envPkgs: []*Package{{
- ImportPath: "github.com/shuLhan/beku",
- }, {
- ImportPath: "github.com/shuLhan/share",
- }},
- importPath: "github.com/shuLhan/share/lib/test",
- exp: true,
- expDeps: []string{
- "github.com/shuLhan/share",
- },
- }, {
- desc: "Is not exist on environment (missing)",
- importPath: "github.com/shuLhan/tekstus",
- exp: true,
- expDeps: []string{
- "github.com/shuLhan/share",
- },
- expDepsMissing: []string{
- "github.com/shuLhan/tekstus",
- },
- expPkgsMissing: []string{
- "github.com/shuLhan/tekstus",
- },
- }, {
- desc: "Is not exist on environment (again)",
- importPath: "github.com/shuLhan/tekstus",
- exp: true,
- expDeps: []string{
- "github.com/shuLhan/share",
- },
- expDepsMissing: []string{
- "github.com/shuLhan/tekstus",
- },
- expPkgsMissing: []string{
- "github.com/shuLhan/tekstus",
- },
- }}
-
- var got bool
-
- gitCurPkg.Deps = nil
- gitCurPkg.DepsMissing = nil
-
- for _, c := range cases {
- t.Log(c.desc)
-
- testEnv.pkgs = c.envPkgs
- testEnv.pkgsMissing = nil
- got = gitCurPkg.addDep(testEnv, c.importPath)
-
- test.Assert(t, "return", c.exp, got, true)
-
- if !got {
- continue
- }
-
- test.Assert(t, "Deps", c.expDeps, gitCurPkg.Deps, true)
- test.Assert(t, "DepsMissing", c.expDepsMissing, gitCurPkg.DepsMissing, true)
- test.Assert(t, "env.pkgsMissing", c.expPkgsMissing,
- testEnv.pkgsMissing, true)
- }
-
- gitCurPkg.Deps = nil
- gitCurPkg.DepsMissing = nil
-}
-
func TestGit(t *testing.T) {
- orgGOPATH := build.Default.GOPATH
-
- testGOPATH, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
-
- testGOPATH += "/testdata"
- build.Default.GOPATH = testGOPATH
-
- defer func() {
- build.Default.GOPATH = orgGOPATH
- }()
-
- err = testInitOutput()
- if err != nil {
- t.Fatal(err)
- }
-
- testEnv, err = NewEnvironment()
- if err != nil {
- t.Fatal(err)
- }
-
- gitCurPkg = NewPackage(testGitRepo, testGitRepo, VCSModeGit)
- gitNewPkg = NewPackage(testGitRepo, testGitRepo, VCSModeGit)
-
- t.Logf("test env : %+v\n", *testEnv)
- t.Logf("gitCurPkg: %+v\n", *gitCurPkg)
- t.Logf("gitNewPkg: %+v\n", *gitNewPkg)
-
t.Run("CompareVersion", testGitCompareVersion)
t.Run("Fetch", testGitFetch)
t.Run("Scan", testGitScan)
t.Run("ScanDeps", testGitScanDeps)
- t.Run("addDep", testAddDep)
}
diff --git a/package_test.go b/package_test.go
index e225bc5..f8dafea 100644
--- a/package_test.go
+++ b/package_test.go
@@ -85,3 +85,101 @@ func TestIsEqual(t *testing.T) {
test.Assert(t, "", c.exp, got, true)
}
}
+
+func TestAddDep(t *testing.T) {
+ cases := []struct {
+ desc string
+ envPkgs []*Package
+ importPath string
+ exp bool
+ expDeps []string
+ expDepsMissing []string
+ expPkgsMissing []string
+ }{{
+ desc: "Is the same path as package",
+ importPath: testGitRepo,
+ }, {
+ desc: "Is vendor package",
+ importPath: "vendor/github.com/shuLhan/beku",
+ }, {
+ desc: "Is standard package",
+ importPath: "os/exec",
+ }, {
+ desc: "Is exist on environment",
+ envPkgs: []*Package{{
+ ImportPath: "github.com/shuLhan/beku",
+ }, {
+ ImportPath: "github.com/shuLhan/share",
+ }},
+ importPath: "github.com/shuLhan/share/lib/test",
+ exp: true,
+ expDeps: []string{
+ "github.com/shuLhan/share",
+ },
+ }, {
+ desc: "Is exist on environment (again)",
+ envPkgs: []*Package{{
+ ImportPath: "github.com/shuLhan/beku",
+ }, {
+ ImportPath: "github.com/shuLhan/share",
+ }},
+ importPath: "github.com/shuLhan/share/lib/test",
+ exp: true,
+ expDeps: []string{
+ "github.com/shuLhan/share",
+ },
+ }, {
+ desc: "Is not exist on environment (missing)",
+ importPath: "github.com/shuLhan/tekstus",
+ exp: true,
+ expDeps: []string{
+ "github.com/shuLhan/share",
+ },
+ expDepsMissing: []string{
+ "github.com/shuLhan/tekstus",
+ },
+ expPkgsMissing: []string{
+ "github.com/shuLhan/tekstus",
+ },
+ }, {
+ desc: "Is not exist on environment (again)",
+ importPath: "github.com/shuLhan/tekstus",
+ exp: true,
+ expDeps: []string{
+ "github.com/shuLhan/share",
+ },
+ expDepsMissing: []string{
+ "github.com/shuLhan/tekstus",
+ },
+ expPkgsMissing: []string{
+ "github.com/shuLhan/tekstus",
+ },
+ }}
+
+ var got bool
+
+ gitCurPkg.Deps = nil
+ gitCurPkg.DepsMissing = nil
+
+ for _, c := range cases {
+ t.Log(c.desc)
+
+ testEnv.pkgs = c.envPkgs
+ testEnv.pkgsMissing = nil
+ got = gitCurPkg.addDep(testEnv, c.importPath)
+
+ test.Assert(t, "return", c.exp, got, true)
+
+ if !got {
+ continue
+ }
+
+ test.Assert(t, "Deps", c.expDeps, gitCurPkg.Deps, true)
+ test.Assert(t, "DepsMissing", c.expDepsMissing, gitCurPkg.DepsMissing, true)
+ test.Assert(t, "env.pkgsMissing", c.expPkgsMissing,
+ testEnv.pkgsMissing, true)
+ }
+
+ gitCurPkg.Deps = nil
+ gitCurPkg.DepsMissing = nil
+}