diff options
| author | Shulhan <ms@kilabit.info> | 2018-05-19 02:39:09 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2018-05-19 02:39:09 +0700 |
| commit | c3daaebcaa04c7e9e7970f80e01046630bc12ed9 (patch) | |
| tree | e1e77361d565eea6f12922fe3cc57489d49b589f | |
| parent | c95d7d0b4f557b531cf58d3b8029d6fd33d85fde (diff) | |
| download | beku-c3daaebcaa04c7e9e7970f80e01046630bc12ed9.tar.xz | |
[test] package: add unit test for String, Update, and UpdateMissingDep
| -rw-r--r-- | env.go | 8 | ||||
| -rw-r--r-- | package.go | 17 | ||||
| -rw-r--r-- | package_git.go | 28 | ||||
| -rw-r--r-- | package_git_test.go | 3 | ||||
| -rw-r--r-- | package_test.go | 204 |
5 files changed, 242 insertions, 18 deletions
@@ -421,7 +421,7 @@ func (env *Env) install(pkg *Package) (ok bool, err error) { // func (env *Env) updateMissing(newPkg *Package) { for x := 0; x < len(env.pkgs); x++ { - env.pkgs[x].UpdateMissingDeps(newPkg) + env.pkgs[x].UpdateMissingDep(newPkg) } var newMissings []string @@ -519,12 +519,6 @@ func (env *Env) postSync(curPkg, newPkg *Package) (err error) { } } - curPkg.ImportPath = newPkg.ImportPath - curPkg.RemoteName = newPkg.RemoteName - curPkg.RemoteURL = newPkg.RemoteURL - curPkg.Version = newPkg.Version - curPkg.isTag = newPkg.isTag - if Debug >= DebugL1 { log.Printf("Package installed:\n%s", curPkg) } @@ -332,7 +332,8 @@ func (pkg *Package) String() string { } // -// Update the current package to the new package remote or version. +// Update the current package to the new package. The new package may contain +// new remote or new version. // func (pkg *Package) Update(newPkg *Package) (err error) { switch pkg.vcs { @@ -343,14 +344,22 @@ func (pkg *Package) Update(newPkg *Package) (err error) { return } + pkg.RemoteName = newPkg.RemoteName + pkg.RemoteURL = newPkg.RemoteURL + pkg.Version = newPkg.Version + pkg.isTag = IsTagVersion(newPkg.Version) + return } // -// UpdateMissingDeps will remove missing package if it's already provided by -// new package, and add it as one of package dependencies. +// UpdateMissingDep will, +// (1) remove missing package if it's already provided by new package +// import-path, +// (2) add it as one of package dependencies of current package, and, +// (3) add current package as required by new package. // -func (pkg *Package) UpdateMissingDeps(newPkg *Package) { +func (pkg *Package) UpdateMissingDep(newPkg *Package) { var missing []string for x := 0; x < len(pkg.DepsMissing); x++ { if !strings.HasPrefix(pkg.DepsMissing[x], newPkg.ImportPath) { diff --git a/package_git.go b/package_git.go index 0aac349..1f4c1a0 100644 --- a/package_git.go +++ b/package_git.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "log" - "os" "os/exec" "github.com/shuLhan/share/lib/ini" @@ -118,21 +117,27 @@ func (pkg *Package) gitGetTagLatest() (tag string, err error) { return } +// +// gitRemoteChange current package remote name (e.g. "origin") or URL to new +// package remote-name or url. +// func (pkg *Package) gitRemoteChange(newPkg *Package) (err error) { + fmt.Println(">>> git remote remove", pkg.RemoteName) cmd := exec.Command("git", "remote", "remove", pkg.RemoteName) cmd.Dir = pkg.FullPath - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + cmd.Stdout = defStdout + cmd.Stderr = defStderr err = cmd.Run() if err != nil { log.Println("gitRemoteChange:", err) } + fmt.Println(">>> git remote add", newPkg.RemoteName, newPkg.RemoteURL) cmd = exec.Command("git", "remote", "add", newPkg.RemoteName, newPkg.RemoteURL) cmd.Dir = pkg.FullPath - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + cmd.Stdout = defStdout + cmd.Stderr = defStderr err = cmd.Run() if err != nil { @@ -199,6 +204,10 @@ func (pkg *Package) gitScanVersion() (version string, err error) { return } +// +// gitUpdate will change the currrent package remote name, URL, or version +// based on new package information. +// func (pkg *Package) gitUpdate(newPkg *Package) (err error) { if pkg.RemoteName != newPkg.RemoteName || pkg.RemoteURL != newPkg.RemoteURL { err = pkg.gitRemoteChange(newPkg) @@ -207,10 +216,15 @@ func (pkg *Package) gitUpdate(newPkg *Package) (err error) { } } + if pkg.Version == newPkg.Version { + return + } + + fmt.Println(">>> git checkout -q", newPkg.Version) cmd := exec.Command("git", "checkout", "-q", newPkg.Version) cmd.Dir = newPkg.FullPath - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + cmd.Stdout = defStdout + cmd.Stderr = defStderr err = cmd.Run() if err != nil { diff --git a/package_git_test.go b/package_git_test.go index d9394fd..515c9f4 100644 --- a/package_git_test.go +++ b/package_git_test.go @@ -110,6 +110,9 @@ func testGitFetch(t *testing.T) { expVersionNext: "v0.2.0", expStdout: `Fetching origin `, + expStderr: `From github.com:shuLhan/beku_test + * [new branch] master -> origin/master +`, }, { desc: "With tag #2", curVersion: "v0.2.0", diff --git a/package_test.go b/package_test.go index 09d8ef0..4505bb5 100644 --- a/package_test.go +++ b/package_test.go @@ -392,3 +392,207 @@ func TestRunGoInstall(t *testing.T) { } } } + +func TestString(t *testing.T) { + cases := []struct { + pkg *Package + exp string + }{{ + pkg: gitCurPkg, + exp: ` +[package "github.com/shuLhan/beku_test"] + VCS = 1 + RemoteName = origin + RemoteURL = git@github.com:shuLhan/beku_test.git + Version = c9f69fb + IsTag = false + Deps = [] + RequiredBy = [] + DepsMissing = [] +`, + }} + + for _, c := range cases { + got := c.pkg.String() + test.Assert(t, "string", c.exp, got, true) + } +} + +func TestUpdate(t *testing.T) { + cases := []struct { + desc string + curPkg *Package + newPkg *Package + expErr error + expPkg *Package + }{{ + desc: "Update remote URL", + curPkg: &Package{ + vcs: VCSModeGit, + ImportPath: testGitRepo, + FullPath: testEnv.srcDir + "/" + testGitRepo, + RemoteName: gitDefRemoteName, + RemoteURL: "https://" + testGitRepo, + }, + newPkg: &Package{ + vcs: VCSModeGit, + ImportPath: testGitRepo, + FullPath: testEnv.srcDir + "/" + testGitRepo, + RemoteName: gitDefRemoteName, + RemoteURL: "git@github.com:shuLhan/beku_test.git", + }, + expPkg: &Package{ + vcs: VCSModeGit, + ImportPath: testGitRepo, + FullPath: testEnv.srcDir + "/" + testGitRepo, + RemoteName: gitDefRemoteName, + RemoteURL: "git@github.com:shuLhan/beku_test.git", + }, + }, { + desc: "Update version", + curPkg: &Package{ + vcs: VCSModeGit, + ImportPath: testGitRepo, + FullPath: testEnv.srcDir + "/" + testGitRepo, + RemoteName: gitDefRemoteName, + RemoteURL: "https://" + testGitRepo, + }, + newPkg: &Package{ + vcs: VCSModeGit, + ImportPath: testGitRepo, + FullPath: testEnv.srcDir + "/" + testGitRepo, + RemoteName: gitDefRemoteName, + RemoteURL: "git@github.com:shuLhan/beku_test.git", + Version: "v0.1.0", + isTag: true, + }, + expPkg: &Package{ + vcs: VCSModeGit, + ImportPath: testGitRepo, + FullPath: testEnv.srcDir + "/" + testGitRepo, + RemoteName: gitDefRemoteName, + RemoteURL: "git@github.com:shuLhan/beku_test.git", + Version: "v0.1.0", + isTag: true, + }, + }, { + desc: "Update version back", + curPkg: &Package{ + vcs: VCSModeGit, + ImportPath: testGitRepo, + FullPath: testEnv.srcDir + "/" + testGitRepo, + RemoteName: gitDefRemoteName, + RemoteURL: "https://" + testGitRepo, + }, + newPkg: &Package{ + vcs: VCSModeGit, + ImportPath: testGitRepo, + FullPath: testEnv.srcDir + "/" + testGitRepo, + RemoteName: gitDefRemoteName, + RemoteURL: "git@github.com:shuLhan/beku_test.git", + Version: "c9f69fb", + isTag: true, + }, + expPkg: &Package{ + vcs: VCSModeGit, + ImportPath: testGitRepo, + FullPath: testEnv.srcDir + "/" + testGitRepo, + RemoteName: gitDefRemoteName, + RemoteURL: "git@github.com:shuLhan/beku_test.git", + Version: "c9f69fb", + isTag: false, + }, + }} + + for _, c := range cases { + t.Log(c.desc) + + testResetOutput(t, false) + + err := c.curPkg.Update(c.newPkg) + + testResetOutput(t, false) + stdout, stderr := testGetOutput(t) + + if err != nil { + t.Log("stderr:", stderr) + test.Assert(t, "err", c.expErr, err.Error(), true) + continue + } + + if len(stdout) > 0 { + t.Log("stdout:", stdout) + } + + test.Assert(t, "current pkg", c.expPkg, c.curPkg, true) + } +} + +func TestUpdateMissingDep(t *testing.T) { + cases := []struct { + desc string + curPkg *Package + misPkg *Package + expCurPkg *Package + expMisPkg *Package + }{{ + desc: "No missing found", + curPkg: &Package{ + ImportPath: "curpkg", + DepsMissing: []string{ + "a", + "b", + }, + }, + misPkg: &Package{ + ImportPath: "c", + }, + expCurPkg: &Package{ + ImportPath: "curpkg", + DepsMissing: []string{ + "a", + "b", + }, + }, + expMisPkg: &Package{ + ImportPath: "c", + }, + }, { + desc: "Missing package found", + curPkg: &Package{ + ImportPath: "curpkg", + DepsMissing: []string{ + "a", + "b", + "c", + }, + }, + misPkg: &Package{ + ImportPath: "c", + }, + expCurPkg: &Package{ + ImportPath: "curpkg", + DepsMissing: []string{ + "a", + "b", + }, + Deps: []string{ + "c", + }, + }, + expMisPkg: &Package{ + ImportPath: "c", + RequiredBy: []string{ + "curpkg", + }, + }, + }} + + for _, c := range cases { + t.Log(c.desc) + + c.curPkg.UpdateMissingDep(c.misPkg) + + test.Assert(t, "", c.expCurPkg, c.curPkg, true) + } +} |
