github.com/ericwq/aprilsh@v0.0.0-20240517091432-958bc568daa0/doc/test.md (about)

     1  # source
     2  [Go: tests with HTML coverage report](https://kenanbek.medium.com/go-tests-with-html-coverage-report-f977da09552d)
     3  
     4  ## This command will run tests for the whole project.
     5  ```sh
     6  go test -cover ./...
     7  ```
     8  
     9  ## In the first command, we use -coverprofile to save coverage results to the file. 
    10  ```sh
    11  go test -coverprofile=coverage.out ./...
    12  ```
    13  
    14  ## we print detailed results by using Go’s cover tool.
    15  ```sh
    16  go tool cover -func=coverage.out
    17  ```
    18  
    19  ## By using the same cover tool, we can also view coverage result as an HTML page
    20  ```sh
    21  go tool cover -html=coverage.out
    22  ```
    23  
    24  ## You can select coverage mode by using -covermode option:
    25  
    26  ```sh
    27  go test -covermode=count -coverprofile=coverage.out
    28  ```
    29  
    30  - set: did each statement run?
    31  - count: how many times did each statement run?
    32  - atomic: like count, but counts precisely in parallel programs
    33  
    34  ## dependency lib
    35  
    36  ```sh
    37  % apk add ncurses foot-extra-terminfo rxvt-unicode-terminfo ncurses-terminfo wezterm-extra-terminfo ncurses-terminfo-base
    38  % apk add build-base autoconf automake gzip libtool ncurses-dev openssl-dev>3 perl-dev perl-io-tty protobuf-dev zlib-dev perl-doc
    39  % apk add libxmu-dev mesa-dev freetype-dev
    40  % apk add musl-locales-lang musl-locales utmps-dev
    41  ```
    42  ## Combined Unit and Integration Code Coverage
    43  
    44  ```sh
    45  rm -rf coverage
    46  mkdir -p coverage/unit -p coverage/int
    47  ```
    48  ### Run unit tests to collect coverage
    49  
    50  ```sh
    51  go test -cover . -args -test.gocoverdir=./coverage/unit
    52  ```
    53  ### Retrieve total coverage
    54  
    55  ```sh
    56  go tool covdata percent -i=./coverage/unit,./coverage/int
    57  ```
    58  
    59  ### Convert total coverage to cover profile
    60  
    61  ```sh
    62  go tool covdata textfmt -i=./coverage/unit,./coverage/int -o coverage/profile
    63  ```
    64  
    65  ### View total coverage
    66  
    67  ```sh
    68  go tool cover -func coverage/profile
    69  go tool cover -html coverage/profile
    70  ```
    71  
    72  ### start server
    73  ```sh
    74  go build -o ~/.local/bin/ashd .
    75  ~/.local/bin/apshd -verbose 1 2>> /tmp/apshd.log
    76  GOCOVERDIR=./coverage/int ~/.local/bin/apshd -verbose 1 2>> /tmp/apshd.log
    77  ```
    78  ### start client
    79  ```sh
    80  docker exec -u ide -it nvide ash
    81  cd develop/aprilsh/frontend/client
    82  go build -o apsh .
    83  go build -race -o apsh .
    84  ./apsh -verbose 1  -pwd password ide@localhost 2>> /tmp/apsh.log
    85  ./apsh -verbose 1 -pwd password ide@172.17.0.3 2>> /tmp/apsh.log
    86  ```
    87  ### pprof
    88  go tool pprof client cpu.profile
    89  go test -bench=. -count 5 -run=^# -benchmem
    90  
    91  ## git branch
    92  
    93  ### create git branch
    94  
    95  ```sh
    96  git branch new-feature                  ## create new branch
    97  git checkout new-feature                ## switch to new branch
    98  ```
    99  
   100  ### commit to git branch
   101  ```sh
   102  git add <changed-files>                 ## add all change to stage
   103  git commit -m "add new feature"         ## commit change to local repo
   104  git push origin new-feature             ## push change to remote repo
   105  ```
   106  
   107  ### merge to main branch
   108  ```sh
   109  git checkout main                       ## switch to main branch
   110  git merge new-feature                   ## merge new branch to main
   111  git push origin main                    ## push main branch to remote repo
   112  ```
   113  
   114  ### solve conflict
   115  ```sh
   116  git add <conflicted-file>               ## add conflict solved files to stage
   117  git commit -m "resolve merge conflict"  ## commit change to local repo
   118  git merge new-feature                   ## merge continue
   119  ```
   120  
   121  ### delete the branch
   122  ```sh
   123  git branch -d new-feature               ## delete local branch
   124  git push origin --delete new-feature    ## delete remote branch
   125  ```