Skip to content

Commit

Permalink
Add unit test for update subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 committed Sep 17, 2022
1 parent 79a000f commit 514386f
Show file tree
Hide file tree
Showing 3 changed files with 384 additions and 5 deletions.
186 changes: 186 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,189 @@ func TestExecute_Import(t *testing.T) {
}
*/
}

func TestExecute_Update(t *testing.T) {
OsExit = func(code int) {}
defer func() {
OsExit = os.Exit
}()

gobin, err := goutil.GoBin()
if err != nil {
t.Fatal(err)
}
if file.IsDir(gobin) {
if err := os.Rename(gobin, gobin+".backup"); err != nil {
t.Fatal(err)
}
defer func() {
if file.IsDir(gobin + ".backup") {
os.RemoveAll(gobin)
if err := os.Rename(gobin+".backup", gobin); err != nil {
t.Fatal(err)
}
}
}()

if err := os.MkdirAll(gobin, 0755); err != nil {
t.Fatal(err)
}

in, err := os.Open("./testdata/check_success/gal")
if err != nil {
t.Fatal(err)
}
defer in.Close()

out, err := os.Create(filepath.Join(gobin, "gal"))
if err != nil {
t.Fatal(err)
}
defer out.Close()

_, err = io.Copy(out, in)
if err != nil {
t.Fatal(err)
}

if err = os.Chmod(filepath.Join(gobin, "gal"), 0777); err != nil {
t.Fatal(err)
}
}

defer func() {
os.RemoveAll("./testdata/.config/fish")
os.RemoveAll("./testdata/.zsh")
os.RemoveAll("./testdata/.zshrc")
os.RemoveAll("./testdata/.bash_completion")
os.RemoveAll("./testdata/.config/gup/assets")
os.RemoveAll("./testdata/go")
os.RemoveAll("./testdata/.cache")
}()

orgStdout := print.Stdout
orgStderr := print.Stderr
pr, pw, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
print.Stdout = pw
print.Stderr = pw

os.Args = []string{"gup", "update"}
Execute()
pw.Close()
print.Stdout = orgStdout
print.Stderr = orgStderr

buf := bytes.Buffer{}
_, err = io.Copy(&buf, pr)
if err != nil {
t.Error(err)
}
defer pr.Close()
got := strings.Split(buf.String(), "\n")

contain := false
for _, v := range got {
if strings.Contains(v, "gup:INFO : [1/1] github.com/nao1215/gal/cmd/gal") {
contain = true
}
}
if !contain {
t.Errorf("failed to update gal command")
}
}

func TestExecute_Update_DryRun(t *testing.T) {
OsExit = func(code int) {}
defer func() {
OsExit = os.Exit
}()

gobin, err := goutil.GoBin()
if err != nil {
t.Fatal(err)
}
if file.IsDir(gobin) {
if err := os.Rename(gobin, gobin+".backup"); err != nil {
t.Fatal(err)
}
defer func() {
if file.IsDir(gobin + ".backup") {
os.RemoveAll(gobin)
if err := os.Rename(gobin+".backup", gobin); err != nil {
t.Fatal(err)
}
}
}()

if err := os.MkdirAll(gobin, 0755); err != nil {
t.Fatal(err)
}

in, err := os.Open("./testdata/check_success/gal")
if err != nil {
t.Fatal(err)
}
defer in.Close()

out, err := os.Create(filepath.Join(gobin, "gal"))
if err != nil {
t.Fatal(err)
}
defer out.Close()

_, err = io.Copy(out, in)
if err != nil {
t.Fatal(err)
}

if err = os.Chmod(filepath.Join(gobin, "gal"), 0777); err != nil {
t.Fatal(err)
}
}

defer func() {
os.RemoveAll("./testdata/.config/fish")
os.RemoveAll("./testdata/.zsh")
os.RemoveAll("./testdata/.zshrc")
os.RemoveAll("./testdata/.bash_completion")
os.RemoveAll("./testdata/.config/gup/assets")
os.RemoveAll("./testdata/go")
os.RemoveAll("./testdata/.cache")
}()

orgStdout := print.Stdout
orgStderr := print.Stderr
pr, pw, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
print.Stdout = pw
print.Stderr = pw

os.Args = []string{"gup", "update", "--dry-run"}
Execute()
pw.Close()
print.Stdout = orgStdout
print.Stderr = orgStderr

buf := bytes.Buffer{}
_, err = io.Copy(&buf, pr)
if err != nil {
t.Error(err)
}
defer pr.Close()
got := strings.Split(buf.String(), "\n")

contain := false
for _, v := range got {
if strings.Contains(v, "gup:INFO : [1/1] github.com/nao1215/gal/cmd/gal") {
contain = true
}
}
if !contain {
t.Errorf("failed to update gal command")
}
}
14 changes: 9 additions & 5 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var updateCmd = &cobra.Command{
If you execute '$ gup update', gup gets the package path of all commands
under $GOPATH/bin and automatically updates commands to the latest version.`,
Run: func(cmd *cobra.Command, args []string) {
os.Exit(gup(cmd, args))
OsExit(gup(cmd, args))
},
}

Expand All @@ -37,21 +37,25 @@ func init() {
func gup(cmd *cobra.Command, args []string) int {
dryRun, err := cmd.Flags().GetBool("dry-run")
if err != nil {
print.Fatal(fmt.Errorf("%s: %w", "can not parse command line argument (--dry-run)", err))
print.Err(fmt.Errorf("%s: %w", "can not parse command line argument (--dry-run)", err))
return 1
}

if err := goutil.CanUseGoCmd(); err != nil {
print.Fatal(fmt.Errorf("%s: %w", "you didn't install golang", err))
print.Err(fmt.Errorf("%s: %w", "you didn't install golang", err))
return 1
}

pkgs, err := getPackageInfo()
if err != nil {
print.Fatal(err)
print.Err(err)
return 1
}
pkgs = extractUserSpecifyPkg(pkgs, args)

if len(pkgs) == 0 {
print.Fatal("unable to update package: no package information")
print.Err("unable to update package: no package information")
return 1
}
return update(pkgs, dryRun)
}
Expand Down
Loading

0 comments on commit 514386f

Please sign in to comment.