Run go test for specific package - go

I used the following command to run test for specific package
go test fts -run run_test.go
can't load package: package fts: cannot find package "fts" in any of:/usr/local/Cellar/go/1.11.1/libexec/src/integration (from $GOROOT)/Users/i055555/go/src/fts (from $GOPATH)
And the package looks like
gitproj/
|---- fts
|---- -command
|---- -run.go
|---- -run_test.go
|---- internal
|---- -fs.go
|---- -tb.go
|---- -tb_test.go
main.go

So you can run go test for a specific package by giving it the relative path:
go test ./fts/command
The --run flag takes a regular expression that helps dictates which tests will be ran within the package.
For example if you had a test named TestFoo(...) and another TestBar(...). go test --run=TestFoo will only run TestFoo(...).

Related

How can I use cli Go files that belong to a web app?

My question is related to the structure or approach of Go applications. I have got the following application.
root
|- app
| |- services
| |- repositories
| |- handlers
| |- commands
|- go.mod
|- main.go
The Go files in the commands package are working independently. The rest of the packages are working for a web application. I start an HTTP web server in the main.go
So, I'd like to run the Go files in the commands packages in the crontab. But as I know, I'll build these whole packages into a single binary file. My question is how can I run the Go files in the commands packages independently in the crontab? I think I should separate them into 2 applications such as "web app" and "command app" but actually they are related to each other and I don't want to manage 2 apps differently. May I use commands Go files in the crontab and on the other hand start an HTTP web server in the main.go?
There's no reason why you can't import packages from your web application module into another one, but if you want to keep them together, you can just do what is quite common, and add additional main packages in specific directories, giving a directory structure like this:
root
|- app
| |-services
| |-...
|- cmd
| |- tools
| | |- main.go
|- main.go
You can build/install your CLI binary simply by running go build ./cmd/tools or go install ./cmd/tools

How to pass arguments via go generate

Here is how my project is structure
├── cmd
│   ├── orders
│   │   ├── main.go
└── scripts
└── codegenerator.go
the codegenerator.go file is the file where i have put my code to generate the code. Here is the logic for codegenerator.go
rename main.go to old_main.go
read from old_main.go line by line and write to a new file called main.go
insert new lines/code blocks based on markers/placeholder in main.go
remove old_main.go file
The go:generate directive is in main.go like this
//go:generate go run ../../scripts/codegenerator.go
I ran go generate from command line and wanted to pass in 3 arguments so that they can be utilized in codegenerator.go. here is my command and the errors i got (i executed this command on path cmd/orders)
$ go generate arg_one arg_two arg_three
can't load package: package arg_one: unknown import path "arg_one": cannot find module providing package arg_one
can't load package: package arg_two: unknown import path "arg_two": cannot find module providing package arg_two
can't load package: package arg_three: unknown import path "arg_three": cannot find module providing package arg_three
So the questions are
Am i running go generate properly?
How can i pass in arguments from go generate command line to the target script
While I agree you may be better off using another solution, there isn't anything preventing usage of env vars (os specifics may vary)
➜ /tmp cat foo.go
package main
//go:generate python run.py $ARG
func main() {}
➜ /tmp cat run.py
import sys
print sys.argv[1]
➜ /tmp ARG=poop go generate foo.go
poop

Run go tests except from one package

I’ve the following project structure and I want to run go test exclude option to run the test
e.g. Command to Run all tests except the test in the cmd package (I’ve more then 3 packages , the struct below is a simple example)
myGithubProject/
|---- cmd
|---- -command
|---- -hello.go
|---- -hello_test.go
|---- internal
|---- -fs.go
|---- -fs.go
|---- -fs_test.go
|---- -log
|---- -log.go
|---- -log_test.go
main.go
If you have a hierarchy like that, you may specify the sibling folder to test (and recurse down) like this:
go test internal/...
If this is not feasible to you (e.g. you have many siblings of cmd, or you have many subfolders inside cmd which you do want to test), you may use build constraints to achieve what you want.
For example, add an exclusion of a donttestme build tag to the hello_test.go file (the first line):
// +build !donttestme
And then when you specify this tag when testing, files that exclude this build tag will not be considered (will be skipped):
go test -tags donttestme <somepackages>

Ginkgo package testing

I'm implementing test suite for brand new go app and decided to use ginkgo. The app has main function and several packages
.
|- main.go
|- types
| |-- user.go
| |-- post.go
|- server_pkg
| |-- users_controller.go
| |-- posts_controller.go
|- worker_pkg
| |-- users_worker.go
| |-- posts_worker.go
I ran ginkgo bootstrap in each package folder and added test files using ginkgo generate. Now I'm able to run tests for each package separately i.e.
cd server_pkg; ginkgo
The question is that: how to configure my application to run all tests for main function and packages using single command?
I can chain commands like ginkgo; cd server_pkg; ginkgo ..., but it does not look like good solution.
To run all test suits you should run this in command in your root catalog
ginkgo -r
Also is good practice as in normal test suits to run all test with race detector, also you could shuffle some test. You can run all this option by using
ginkgo -r --race --randomizeAllSpecs --randomizeSuites
#ttomalak thank you! It's exactly what I wanted
$ ginkgo -r

go build works fine but go run fails

I have a few files in the main package under one directory:
main.go
config.go
server.go
When I do: "go build" the program builds perfect and runs fine.
When I do: "go run main.go" it fails.
Output:
# command-line-arguments
./main.go:7: undefined: Config
./main.go:8: undefined: Server
The symbols that are undefined are structs and they are capitalised so should be exported.
My Go version: go1.1.2 linux/amd64
This should work
go run main.go config.go server.go
Go run takes a file or files and it complies those and only those files which explains the missing symbols in the original post.
You could execute it as:
go run .
so you don't have to include all files manually.
Good practise would be to create a package for it and run
go run ./app
Ex. Folder Structure
├──app/
| ├──main.go
| ├──config.go
| ├──server.go
├──bin/
| ├──foo.go
└──pkg/
└──bar.go

Resources