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
Related
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
I am trying to serve my frontend using the Gin framework. It is a small project which would make maintenance easier having it as a single binary.
The project structure looks like this:
Project
|
+-- backend
| |
| +-- backend (binary)
|
+-- frontend
| |
| +-- dist
| |
| +-- package.json
|
+-- Procfile
|
+-- .gitlab-ci.yml
Currently I am serving the fronend like this:
r.Use(static.Serve("/", static.LocalFile("../frontend/dist", false)))
For local dev this works fine and I did not have any issues. If I deploy this with my gitlab-ci pipeline it fails as I do not upload the ../frontend/dist directory. I looked at the pkgr library which should help me achieve my goal.
My issue is I can not get it to work with the Gin framework. Current snippet:
test := pkger.Dir("../frontend/dist")
r.Use(static.Serve("/", static.LocalFile("../frontend/dist", false))) <- compiles but does not serve frontend
r.Use(static.Serve("/", static.LocalFile(test, false))) <- Does not compile
Is there an easier way to achieve my goal?
The problem is when you deploy the project to Heroku (I supposed you are using Heroku because of the tag you are using) it compiles the Go to a binary.
The binary is running in the root folder of the project and ../frontend is not in the same path as the development enviroment anymore.
Something like this:
Project
|
+-- backend
| |
| +-- backend (binary)
|
+-- frontend
| |
| +-- dist
| |
| +-- package.json
|
+-- Procfile
|
+-- .gitlab-ci.yml
|
+-- project binary <---- here
So the correct path is ./frontend/ (see one dot)
You can set an environment variable to set the correct path.
As you are using Vue, you can set for the development environment a dev-server with a proxy pointing to backend. Then when building for production you can make Vue compiles to a specific folder that is hardcoded and served by the backend.
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(...).
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>
Hi all. I'm very new with Go and Gogland. I have a project
I choose "Run kind" as Package - to run not only main file but a project. Why it cannot find main package??
How to import util.myprinter package to main.go to use it??
Please, help me
First, the general structure of your Go workspace seems to be wrong. You need to make it look more like this:
D:
|-- go_projects
| |-- bin
| |-- pkg
| |-- src
| | |-- FirstSteps
| | | |-- main.go
| | | +-- util
| | | +-- myprinter.go
| | |-- SecondProject
| | |-- ThirdProject
...
Second your import statement seems to be empty, I have no idea how GoLand works but if you want to use whatever is in your myprinter.go file, you will need to import the util package, assuming that the myprinter.go file declares its package as util at the top.
// FirstSteps/main.go
package main
import (
"FirstSteps/util"
)
func main() {
util.MyPrinterFunc()
}
And of course to be able to use anything from util there first must be something...
// FirstSteps/util/myprinter.go
package util
func MyPrinterFunc() {
// do stuff...
}
Edit: I'm sorry, I didn't actually answer your question initially. You're getting the error Cannot find package 'main' because of the wrong workspace setup I already mentioned. The Package path tells GoLand where the package you want to run is relative to the $GOPATH/src directory. So after you've setup your wrokspace correctly, you should set the Package path to FirstSteps since that package's absolute path will be $GOPATH/src/FirstSteps. If, later, you want to run the util package you would specify Package path as FirstSteps/util for GoLand to be able to find it.