Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for all subcommand #42

Merged
merged 10 commits into from
Sep 17, 2022
Merged

Add unit test for all subcommand #42

merged 10 commits into from
Sep 17, 2022

Conversation

nao1215
Copy link
Owner

@nao1215 nao1215 commented Sep 17, 2022

No description provided.

@nao1215 nao1215 changed the title Add unit test for remove subcommand's success case Add unit test for all subcommand Sep 17, 2022
@codecov
Copy link

codecov bot commented Sep 17, 2022

Codecov Report

Merging #42 (514386f) into main (5d5c188) will increase coverage by 37.09%.
The diff coverage is 77.14%.

@@             Coverage Diff             @@
##             main      #42       +/-   ##
===========================================
+ Coverage   47.31%   84.40%   +37.09%     
===========================================
  Files          10       10               
  Lines         279      295       +16     
===========================================
+ Hits          132      249      +117     
+ Misses        145       33      -112     
- Partials        2       13       +11     
Impacted Files Coverage Δ
cmd/import.go 57.14% <55.55%> (+45.37%) ⬆️
cmd/update.go 67.77% <55.55%> (+53.82%) ⬆️
cmd/export.go 89.65% <100.00%> (+85.65%) ⬆️
cmd/remove.go 91.42% <100.00%> (+84.97%) ⬆️
cmd/check.go 96.29% <0.00%> (+5.55%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@nao1215 nao1215 merged commit b515c5a into main Sep 17, 2022
@KEINOS
Copy link
Contributor

KEINOS commented Sep 17, 2022

@nao1215

I don't know why the CI passed, but this commit b515c5a seems to fail on local testing.
Both on macOS and Linux (Docker: golang:1.18 & golang:latest).

$ sw_vers
ProductName:    macOS
ProductVersion: 12.5
BuildVersion:   21G72

$ go version
go version go1.19.1 darwin/amd64

$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

$ git show
commit b515c5abdf5385ee3ad23ceabe44d1aa50bc6347 (HEAD -> main, upstream/main, upstream/HEAD, origin/main, origin/HEAD)
Merge: d25b452 514386f
Author: CHIKAMATSU Naohiro <[email protected]>
Date:   Sat Sep 17 16:00:07 2022 +0900

    Merge pull request #42 from nao1215/add-unit-tests-for-subcmd
    
    Add unit test for all subcommand
Logs
$ go test ./...
?       github.com/nao1215/gup  [no test files]

gup:WARN : can't get 'test'package path information. old go version binary
--- FAIL: Test_export (0.02s)
    --- FAIL: Test_export/can_not_make_.config_directory (0.00s)
        export_test.go:196: value is mismatch (-want +got):
              []string{
                strings.Join({
                        "gup:ERROR: can not make config directory: mkdir /.config: ",
            -           "permission denied",
            +           "read-only file system",
                }, ""),
                "",
              }
gup:ERROR: /.config/gup/gup.conf is not found
gup:ERROR: can not parse command line argument: flag accessed but not defined: dry-run
gup:ERROR: testdata/empty_conf/.config/gup/gup.conf is not found
gup:ERROR: testdata/can_not_read_conf/.config/gup/gup.conf is not found
gup:ERROR: no such file or directory: testdata/delete/posixer.exe
gup:CHECK: remove testdata/delete/posixer? [Y/n] gup:INFO : removed testdata/delete/posixer
gup:CHECK: remove testdata/delete/posixer? [Y/n] gup:INFO : cancel removal testdata/delete/posixer
gup command update binaries installed by 'go install'.
If you update all binaries, just run '$ gup update'

Usage:
  gup [command]

Available Commands:
  check       Check the latest version of the binary installed by 'go install'
  export      Export the binary names under $GOPATH/bin and their path info. to gup.conf.
  help        Help about any command
  import      Install command according to gup.conf.
  list        List up command name with package path and version under $GOPATH/bin or $GOBIN
  remove      Remove the binary under $GOPATH/bin or $GOBIN
  update      Update binaries installed by 'go install'
  version     Show gup command version information

Flags:
  -h, --help   help for gup

Use "gup [command] --help" for more information about a command.
Error: unknown flag: --no-exist-option
Usage:
  gup [command]

Available Commands:
  check       Check the latest version of the binary installed by 'go install'
  export      Export the binary names under $GOPATH/bin and their path info. to gup.conf.
  help        Help about any command
  import      Install command according to gup.conf.
  list        List up command name with package path and version under $GOPATH/bin or $GOBIN
  remove      Remove the binary under $GOPATH/bin or $GOBIN
  update      Update binaries installed by 'go install'
  version     Show gup command version information

Flags:
  -h, --help   help for gup

Use "gup [command] --help" for more information about a command.

gup:ERROR: unknown flag: --no-exist-option

gup:INFO : removed testdata/delete/posixer
gup:INFO : Export /Users/admin/.config/gup/gup.conf
gup:INFO : create bash-completion file: testdata/.bash_completion
gup:INFO : create fish-completion file: testdata/.config/fish/completions/gup.fish
gup:INFO : create zsh-completion file: testdata/.zsh/completion/_gup
gup:ERROR: testdata/.config/gup/gup.conf is not found
--- FAIL: TestExecute_Update (0.00s)
    root_test.go:476: rename /Users/admin/go/bin /Users/admin/go/bin.backup: file exists
--- FAIL: TestExecute_Update_DryRun (0.00s)
    root_test.go:569: rename /Users/admin/go/bin /Users/admin/go/bin.backup: file exists
gup:WARN : not found 'test4' package in $GOPATH/bin or $GOBIN
FAIL
FAIL    github.com/nao1215/gup/cmd      1.001s
?       github.com/nao1215/gup/internal/assets  [no test files]
?       github.com/nao1215/gup/internal/cmdinfo [no test files]
?       github.com/nao1215/gup/internal/completion      [no test files]
?       github.com/nao1215/gup/internal/config  [no test files]
?       github.com/nao1215/gup/internal/file    [no test files]
?       github.com/nao1215/gup/internal/goutil  [no test files]
?       github.com/nao1215/gup/internal/notify  [no test files]
ok      github.com/nao1215/gup/internal/print   (cached)
ok      github.com/nao1215/gup/internal/slice   (cached)
FAIL

To reproduce

If you have Docker installed, here's the Dockerfile to reproduce.

FROM golang:1.18

WORKDIR /go/src

RUN \
    git clone https://github.com/nao1215/gup.git && \
    cd gup && \
    go mod download && \
    go test ./...
Logs
$ docker build -t test:local .
[+] Building 17.1s (6/6) FINISHED                                                                                                                                                              
 => [internal] load build definition from Dockerfile                                                                                                                                      0.0s
 => => transferring dockerfile: 37B                                                                                                                                                       0.0s
 => [internal] load .dockerignore                                                                                                                                                         0.0s
 => => transferring context: 2B                                                                                                                                                           0.0s
 => [internal] load metadata for docker.io/library/golang:1.18                                                                                                                            0.0s
 => [1/3] FROM docker.io/library/golang:1.18                                                                                                                                              0.0s
 => CACHED [2/3] WORKDIR /go/src                                                                                                                                                          0.0s
 => ERROR [3/3] RUN     git clone https://github.com/nao1215/gup.git &&     cd gup &&     go mod download &&     go test ./...                                                           17.0s
------                                                                                                                                                                                         
 > [3/3] RUN     git clone https://github.com/nao1215/gup.git &&     cd gup &&     go mod download &&     go test ./...:                                                                       
#6 0.233 Cloning into 'gup'...                                                                                                                                                                 
#6 13.94 ?      github.com/nao1215/gup  [no test files]                                                                                                                                        
#6 16.77                                                                                                                                                                                       
#6 16.77 gup:WARN : can't get 'test'package path information. old go version binary                                                                                                            
#6 16.77 --- FAIL: Test_export (0.03s)
#6 16.77     --- FAIL: Test_export/can_not_make_.config_directory (0.00s)
#6 16.77         export_test.go:196: value is mismatch (-want +got):
#6 16.77               []string{
#6 16.77               	strings.Join({
#6 16.77               		"gup:ERROR: ",
#6 16.77             - 		"can not make config directory: mkdir /.config: permission denied",
#6 16.77             + 		"no package information",
#6 16.77               	}, ""),
#6 16.77               	"",
#6 16.77               }
#6 16.77 gup:ERROR: /.config/gup/gup.conf is not found
#6 16.77 gup:ERROR: can not parse command line argument: flag accessed but not defined: dry-run
#6 16.77 gup:ERROR: testdata/empty_conf/.config/gup/gup.conf is not found
#6 16.77 gup:ERROR: testdata/can_not_read_conf/.config/gup/gup.conf is not found
#6 16.77 gup:ERROR: no such file or directory: testdata/delete/posixer.exe
#6 16.77 gup:CHECK: remove testdata/delete/posixer? [Y/n] gup:INFO : removed testdata/delete/posixer
#6 16.77 gup:CHECK: remove testdata/delete/posixer? [Y/n] gup:INFO : cancel removal testdata/delete/posixer
#6 16.77 gup:INFO : create bash-completion file: /root/.bash_completion
#6 16.77 gup:INFO : create fish-completion file: /root/.config/fish/completions/gup.fish
#6 16.77 gup:INFO : create zsh-completion file: /root/.zsh/completion/_gup
#6 16.77 gup command update binaries installed by 'go install'.
#6 16.77 If you update all binaries, just run '$ gup update'
#6 16.77 
#6 16.77 Usage:
#6 16.77   gup [command]
#6 16.77 
#6 16.77 Available Commands:
#6 16.77   check       Check the latest version of the binary installed by 'go install'
#6 16.77   export      Export the binary names under $GOPATH/bin and their path info. to gup.conf.
#6 16.77   help        Help about any command
#6 16.77   import      Install command according to gup.conf.
#6 16.77   list        List up command name with package path and version under $GOPATH/bin or $GOBIN
#6 16.77   remove      Remove the binary under $GOPATH/bin or $GOBIN
#6 16.77   update      Update binaries installed by 'go install'
#6 16.77   version     Show gup command version information
#6 16.77 
#6 16.77 Flags:
#6 16.77   -h, --help   help for gup
#6 16.77 
#6 16.77 Use "gup [command] --help" for more information about a command.
#6 16.77 gup:INFO : append bash-completion: /root/.bash_completion
#6 16.77 Error: unknown flag: --no-exist-option
#6 16.77 Usage:
#6 16.77   gup [command]
#6 16.77 
#6 16.77 Available Commands:
#6 16.77   check       Check the latest version of the binary installed by 'go install'
#6 16.77   export      Export the binary names under $GOPATH/bin and their path info. to gup.conf.
#6 16.77   help        Help about any command
#6 16.77   import      Install command according to gup.conf.
#6 16.77   list        List up command name with package path and version under $GOPATH/bin or $GOBIN
#6 16.77   remove      Remove the binary under $GOPATH/bin or $GOBIN
#6 16.77   update      Update binaries installed by 'go install'
#6 16.77   version     Show gup command version information
#6 16.77 
#6 16.77 Flags:
#6 16.77   -h, --help   help for gup
#6 16.77 
#6 16.77 Use "gup [command] --help" for more information about a command.
#6 16.77 
#6 16.77 gup:ERROR: unknown flag: --no-exist-option
#6 16.77 
#6 16.77 gup:INFO : append bash-completion: /root/.bash_completion
#6 16.77 gup:INFO : append bash-completion: /root/.bash_completion
#6 16.77 gup:INFO : removed testdata/delete/posixer
#6 16.77 gup:INFO : append bash-completion: /root/.bash_completion
#6 16.77 gup:INFO : Export /root/.config/gup/gup.conf
#6 16.77 gup:INFO : create bash-completion file: testdata/.bash_completion
#6 16.77 gup:INFO : create fish-completion file: testdata/.config/fish/completions/gup.fish
#6 16.77 gup:INFO : create zsh-completion file: testdata/.zsh/completion/_gup
#6 16.77 gup:ERROR: testdata/.config/gup/gup.conf is not found
#6 16.77 --- FAIL: TestExecute_Update (0.00s)
#6 16.77     root_test.go:476: rename /go/bin /go/bin.backup: invalid cross-device link
#6 16.77 --- FAIL: TestExecute_Update_DryRun (0.00s)
#6 16.77     root_test.go:569: rename /go/bin /go/bin.backup: invalid cross-device link
#6 16.77 gup:WARN : not found 'test4' package in $GOPATH/bin or $GOBIN
#6 16.77 FAIL
#6 16.77 FAIL	github.com/nao1215/gup/cmd	1.908s
#6 16.77 ?   	github.com/nao1215/gup/internal/assets	[no test files]
#6 16.77 ?   	github.com/nao1215/gup/internal/cmdinfo	[no test files]
#6 16.77 ?   	github.com/nao1215/gup/internal/completion	[no test files]
#6 16.77 ?   	github.com/nao1215/gup/internal/config	[no test files]
#6 16.77 ?   	github.com/nao1215/gup/internal/file	[no test files]
#6 16.77 ?   	github.com/nao1215/gup/internal/goutil	[no test files]
#6 16.77 ?   	github.com/nao1215/gup/internal/notify	[no test files]
#6 16.77 ok  	github.com/nao1215/gup/internal/print	0.011s
#6 16.77 ok  	github.com/nao1215/gup/internal/slice	0.008s
#6 16.77 FAIL
------
executor failed running [/bin/sh -c git clone https://github.com/nao1215/gup.git &&     cd gup &&     go mod download &&     go test ./...]: exit code: 1

I haven't looked into it in depth, but I have a feeling it is related to this line.

if err := os.Setenv("HOME", "/"); err != nil {

It seems to set the HOME dir to the root. Isn't it safer to set to the temp directory from t.TempDir()?

@nao1215
Copy link
Owner Author

nao1215 commented Sep 17, 2022

@KEINOS
This error occurs because the error message for no write permission is different between macOS and Linux. Error messages returned by the code in the layer close to the system call are different between macOS and Linux.

       export_test.go:196: value is mismatch (-want +got):
              []string{
                strings.Join({
                        "gup:ERROR: can not make config directory: mkdir /.config: ",
            -           "permission denied",
            +           "read-only file system",
                }, ""),
                "",

However, reproducing the error using Docker may present a different problem. I'm pretty sure the test code is wrong.

@nao1215
Copy link
Owner Author

nao1215 commented Sep 17, 2022

@KEINOS
Please see #43 .
I don't have a Mac, so the bug may not be fixed. If so, I will revert #43 .
(The error that occurs in Docker should not be fixed. This is different problem)

@KEINOS
Copy link
Contributor

KEINOS commented Sep 18, 2022

@nao1215

As you said, sadly #43 9e3fc5e did not fix the error on macOS. 😭

This happens while implementing tests. I understand.

Error messages returned by the code in the layer close to the system call are different between macOS and Linux.

I agree. That happens between Windows and macOS too. Many times 😄

My usual work-around for this, to capture error messages during test, is to wrap the error. I use "github.com/pkg/errors" to wrap the error in the function before returning it.

E.g.:

func doSomething(pathDir string) error {
-   return os.MkdirAll(pathDir, 0775)
+   if err := os.MkdirAll(pathDir, 0775); err != nil {
+     errors.Wrap(err, "failed to make directory")
+   }
    // do the thing
}

Like so, the test needs to check only the "failed to make directory" message. You don't need to think about the OS dependent messages.

Another useful package that I found while using cobra is github.com/zenizh/go-capturer (formally known as github.com/kami-zh/go-capturer). It allows you to capture STDOUT and STDERR very simple.

Solving these problems one by one should raise gup's profile and demonstrate that it is a reliable software.
Let's fix them! 💪

@nao1215 nao1215 deleted the add-unit-tests-for-subcmd branch September 18, 2022 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants