Use relative path in init function cannot be found in test - testing

I'm running into an annoying problem when using relative path in the init function which cannot be found by the unit test.
Say I have a project structured like:
.
├── conf
│   └── blacklist
├── filter
│   ├── filter.go
│   └── filter_test.go
And in the init function of filter.go, I try to load the blacklist by using relative path conf/blacklist to avoid loading it multiple times.
Since the default working directory is exactly the project root directory, it works well with compiled binary. However filter_test.go will panic by
panic: open conf/blacklist: no such file or directory, becasue go test always uses the package directory as the working directory.
I also referred this solution, which could dynamically build the relative path to find the config file. It did work well until I add the covermode=set flag and try to do some coverage testing. It seems that the covermode would rewrite and generate some middle packages like _obj, which makes the dynamical relative path not working any more.
Has anyone run into the same problem before? I'm still new to golang for a couple of months. Any suggestions will be appreciated.

A simple solution is to run tests using something like SRC_ROOT=$PWD go test and then inside the test that want to access files use os.Getenv("SRC_ROOT").

Related

Relative link from readme.md to another file in package, rendered in npmjs

I've created an npm module which is hosted on npmjs.com.
Due to the structure of the module, I've split the documentation into a general readme.md and then more specific files which are linked to from the main readme.md:
.
├──documentation
│ ├── doc1.md
│ └── doc2.md
└── readme.md
The links in readme.md look like this: [some text](documentation/doc1.md). They work perfectly fine locally and in their CodeCommit repo (and I suspect it would be fine in github as well).
However after publishing it to npm, the links become https://www.npmjs.com/package/documentation/doc1.md, which of course is an invalid URL and leads to a 404 error.
I've tried to figure out how to get a valid url to the file in question so I can create links that lead to it, but I can't even find out how to get a direct link to the file in the package. It may also be worth noting that the package itself is a private organisation package, but I've tried the same thing on a test public package and had the same issue there, so I don't think it's relevant.
Does anyone know how to link from the root readme.md file to another file in the same package, in a way that works in npmjs?
I've found the issue at npm community they say that it's solved, so I tried for my self to see what went wrong?
And they were right, I've missed some points so I'll write all the required steps:
Make sure your repository at github/gitlab is public
Make sure in your package.json file you have the proper repository configuration that point at your repo correctly
Example:
{
...
"repository": {
"url": "https://github.com/lalosh/js-library-boilerplate",
"type": "git"
}
...
}
When doing the relative linking you need to include the relative path from your repo root, including the .md suffix.
Example:
We can [link](documentation/sub-doc.md) to an inner relative file

What is the recommended way to point an npm package's imports to a specific directory?

I have an npm package structured as follows:
my-package
├── build
├── many.config.files
├── package.json
└── src
It made sense to put helper files such as config files and scripts into the root of my package. Then it made sense to have a src file so that my source files wouldn't mix with all my config files. Then it made sense to have a build directory where the built version of src would get populated (maybe some transpiling and minification happens, etc).
Now my src directory has various JS files and folders, all of which users of my library should be able to import.
src
├── file1.js
├── file2.js
└── utils
For example, import utils3 from "my-package/utils/file3.js" should work just fine for them. They should not need to type import utils3 from "my-package/build/utils/file3.js" to import the file.
What is the best practice for dealing with this situation?
I have tried/considered various approaches:
Setting "main": "./build" in my package.json. This did not resolve the import error for me.
Setting "directories": {"utils": "./build/utils"} in my package.json. This did not resolve the import error for me.
Copying package.json into my build directory during build. Then npm publishing the build directory instead of the entire project. This has the upside of only sharing the built code. This has the downside that the built package will have information in its package.json that doesn't make sense because files/directories are missing/mislocated.
Something along the lines of module.exports = require('./build') in an index.js file. This did not work either.

Robot Framework Suite

I have 15 separate API Tests in Robot Framework. I want to create a Robot Test Suite and have all 15 tests within this suite so that I can run the .robot suite once and have all 15 tests run.
So suppose I have 3 separate tests :
1. Login.robot
2. Get_customer_data.robot
3. Get_product_info.robot and I want to put all these tests in one suite such that when I run the suite it runs the three tests at once. Also, we use mocked data instead of a database so all the mocked data files currently lie within the respective test folders.
For ex: theres a folder called Login which contains login_data (data files) and login.robot (Robot file).
I was thinking I will create a suite with all the .robot files and retain the data files in the respective folders for the suite to access them when the suite is run. I was wondering if this is right and if there is a document that could help me figure this out. Please Help
Could someone kindly help?
Your question mentions having 15 different tests it appears that what you really have are 15 different robot test suites (ie: 15 different files with the .robot suffix) based on a comment you wrote.
The simplest way to combine them into a suite that you can run all at once is to put them in a directory. You can then tell robot to run the directory and it will find all of the .robot files and run them.
For example:
tests
└── api
├── Get_customer_data
│   ├── customer_data
│   └── Get_customer_data.robot
├── Get_product_info
│   ├── Get_product_info.robot
│   └── product_data
├── Login
│   ├── login_data
│   └── Login.robot
└── ...
If you cd to the tests folder, you can do robot api and it will run all of the .robot files in that folder. Or, from the parent of the tests folder you can do robot tests/api.
Test suite directories are thoroughly documented in the robot framework user guide, in a section titled Test suite directories
There's actually an easy way to do this from the command line using pybot. I've verified this with Pycharm.
First, put all of your .robot files in a single folder. Then on the command line, instead of typing pybot TestSuite/Test1.robot, type pybot TestSuite/*. This will run all of the tests in (I observed) alphabetical order.
It may be alphabetical order because that's the order they were in my folder and not because it actually runs them that way, but to fix that prefix each test with a number, like 1_Login.robot, 2_Get_customer_data.robot ,3_Get_product_info.robot. This will execute everything in the numerical order. If you have more than 10, use 01, 02, 03, etc. to make sure everything works properly.
To have a one-click action to run this, you could put that line into a .cmd file, but that may be outside the scope of your question.

GO in IntelliJ IDEA. Multiple File and Error Undefined: Data

I want use IntelliJ IDE Community Edition to write code in GO (GoLang). I instaled right plugin, and instaled all need tools to build application.
My application consists with two below file. Each is in direcytory ../EventServer.
Main.go
Data.go
If I want to run project from IntelliJ using function Run (Ctlr+Shift+F10) and I get below error
/usr/lib/go/bin/go build -o "/tmp/Build Main.go and run0go" -gcflags "-N -l" /my/home/blah/EventServer/Main.go
# command-line-arguments
./Main.go:11: undefined: Data
I can without any problem compiled code from terminal come in to direcytory with project and execution command
:~/Pulpit/EventServer$ go build
./EventServer
Hello
dane w strukturze someone
tree direcytory and files looks like
EventServer$ tree -a
.
├── Data.go
├── EventServer
├── EventServer.iml
├── .idea
│   ├── compiler.xml
│   ├── copyright
│   │   └── profiles_settings.xml
│   ├── libraries
│   │   └── GOPATH__EventServer_.xml
│   ├── misc.xml
│   ├── modules.xml
│   ├── .name
│   ├── vcs.xml
│   └── workspace.xml
└── Main.go
I suppose that command to run is bad, because compiler trying build program with only one file Main.go but not with all files. Right command should be
$ go run *.go
But I do not know where Can I set this.
I also set GOPATH to:
export GOPATH=$HOME/Pulpit/EventServer
This also hasn't help
CODE
Main.go
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello")
abcd := Data{"someone" , "1234567"}
fmt.Printf("dane w strukturze %s ", abcd.Name)
}
And Data.go
package main
type Data struct {
Name string
Phone string
}
SYSTEM: LINUX
----------------------SOLVED-------------------------------------------SOLVED---------------------
Steps
Project must be found in directory for/example/MyProject/src/HERE_DIRECTORY_WITH_YOUR_SOURCE_GO_FILE
sub direcytory src is important
Go to Run --> Edit Configurations
Find below position
Change Run Kind to Package
In Position Package write Your folder with Your code (Shold be Highlight it is correct)
Click On PLUS icon in left top corner, and Add Go Application
Apply changes
In the right top corner main window IDE You see small icon Play
Chose early defined Go Application my is Unamed
Click Play
An Joy :D
Let say you are having a project with src/ sub directory and two .go files inside: hello.go and typetest.go and both defines the same package "main". One of them (hello.go) also implements func main().
To make it compile as whole you need to make sure two things are configured properly: GOPATH and Run/Debug configuration.
Open the Project Libraries/GOPATH settings:
For Gogland
File -> Settings -> Go
For Intellij IDEA
File -> Settings -> Languages & Frameworks -> Go -> Go Libraries
Makes sure the GOPATH for the project looks something like that:
GOPATH settings
Next, open Run -> Edit Configurations and make sure your configuration looks similar to this:
Run/Debug configuration
You can add second file (in your case Data.go) as Go tool arguments field in Run/Debug Configurations. I read about this here: https://github.com/go-lang-plugin-org/go-lang-idea-plugin/issues/2013 near the end of discussion. Example below (I used 2 files HelGo.go and aaa.go) worked for me:
Go Run Configuration with 2 files
Intellij objected in these 2 files were in different folders, and so both of them has to be same package (main).
On the other side I couldn't make any advises on this page work at all.
I solved the same problem with a complete list of checking. Usually, there are multiple problems combined with each other.
First of all, check your GOPATH settings, there are two ways:
use the command line to check your go environment:
$ go env
you will get a global environment setting, make sure your GO folders are correct. If you are using some local packages, be aware of them too.
check your Build tool setting, whether you have added all your resources files into your dependencies. This is another way to set up your project GOPATH, and do not affects the global settings.
Secondly, check your Run/Debug configuration, and make sure your settings have located the main package or main file. No matter which kind of configuration you use, this is always the start.
Sarp Kaya, just follow Mbded steps. Additional step is, ensure that your additional GOPATH should be there.
For example, this is our ~/.profile GOPATH
export GOPATH=$HOME/lib/Go:$HOME/Development/Go
The first path used by go get processes etc, while your active go development directory goes to the next path.
According to our config, the RightApp exact path should be $HOME/Development/Go/src/RightApp.

How to use grunt-contrib-copy to copy to root AND change directory structure

I have an express app with my dev views in /assets/views. I figure I need to separate development and production views because in production I'll be editing the HTML when I used grunt-contrib-usemin to concat/uglify scripts.
So here's the problem. My current tree:
assets/views
├── 404.html
├── index.html
├── layout.html
├── question_ask.html
└── question_display.html
Ideally, I want my production-ready views to live on the same level as assets. Using grunt-contrib-copy, it seems to copy the whole tree. I currently am putting it into public since I'm not sure how to set my dest to the root of the project.
copy: {
views: {
src: ['assets/views/*.html'],
dest: 'public/'
}
So there are a few questions here:
Is it bad practice to have dev views and production views? If so, is there another way of producing a view that has references to concat/uglified scripts?
How the heck can I use grunt-contrib-copy to copy to the root of my project? I don't want assets/views obviously, I just want a views/* folder that has the contents of what's in assets/views/*.
Thanks!
You need to specify the flatten option which will remove the directory structure from the destination path. See my answer here: Copy all files from directory to another with Grunt.js copy