Go unit test not finding required file - unit-testing

I have written a unit test in go and the init method of that same file opens a file in the root of the project. The problem that I am having is that when I run the test the package of the test is the root directory which does not contain the required file. How can I tell the test function to see the file without creating a duplicate file in the directory of the test file?
File structure:
main.go
|+-helpers
|+-data.go
|+-data_test.go
required_file.txt
Test command:
go test github.com/testproj/helpers
Code in data.go:
func init() {
file, err := os.Open("required_file.txt")
if err != nil {
log.Fatal(err) <-- required_file.txt not found
}
}

Your fundamental problem is that you have two different packages that are too tightly coupled. If you have a package which depends for its operation on a resource contained in another package, then really either:
They shouldn't be two different packages; or
You need to decouple them further.
So you have a number of alternatives, here:
Just don't have a separate "helpers" package. If the functionality in that package has no purpose other than to help out with the main package, then combine them, since conceptually they're not independent enough to be distinct packages. As you're seeing, using folders simply as a means to organize code isn't as good an idea in Go as it can be in some other languages.
Fully qualify the file name with an absolute path in both your main and your helpers packages. If it is really true that two independent packages depend on the same file, then they should both be able to find it regardless of where either package happens to be located, and that requires an absolute path. This is probably your least optimal solution.
Further decouple the helpers package so that it doesn't depend on this file anymore, perhaps by:
computing a fully qualified file name in your main package and passing it to the functions in your helpers package when you call them;
opening the file in your main package, and passing an io.Reader to the file to the functions in your helpers package when you call them;
opening the file in your main package, and passing the contents of the file to the functions in your helpers package when you call them, perhaps as a bytes buffer; or
extract the information you need from the file in your main package, and pass only that relevant information to the functions in your helpers package when you call them.
Note that you're going to run into problems eventually with this approach anyway. When you run your main application, your current working directory will by default be whatever directory you are currently in when you execute the program. So unless you fully qualify the filename, then as soon as you run your application from any directory other than the one your source code is in, then it's not going to be able to find the required file, either.

Related

gtest unit tests target configuration file path

In my C++ application, I have a text file (dataFile.txt) that is installed on the Linux target machine in the following path:
/SoftwareHomeDir/Configuration/Application/dataFile.txt
This file exists on my Rational ClearCase source code environment under the path:
/ProjectName/config/Application/dataFile.txt
I am developping a unitTest in gtest that does following:
Read a specific data from dataFile.txt , if the data does not exist than write it into the file.
1) I am avoiding to create an environment variable to check whether I am in the compilation environment or the target machine. Then add additional test code in the final release. I really want to separate test code from final code.
2) I am not using any IDE (no visual studio, no qt, etc.), just notepad++
3) The compilatio. server is shared (access with a username, however the root folder "/" is shared. Which means that if I create the path "/SoftwareHomeDir/Confiugration/Application/dataFile.txt", it will be visible by all users, and if another user is running his gtest unitTest, he may overwrite my file.
4) In the final code, the path to the dataFile is hard coded, and it is very costly (will take few seconds to run) to implement a filesearch(filename) method to look for the file in the entire hard drive before reading the file.
Question:
I am looking for a solution to unit-test my code in the compilation environment that is using /ProjectName/config/Application/dataFile.txt
The solution to my problem was to combine gmock with gtest as described by the link
https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#delegating-calls-to-a-fake
The only modification I made to my code is that instead of defining the path to the configuration data using #define, I created a function getConfigFilePath() that returns the hardcoded path of the configuration file in the installed application. From here, I mocked the class and in my mock, I call a fake getConfigFilePath() that returns, when the real code is executing, the hardcoded path of the config file in the project tree in ClearCase. This is precisely what I was looking for.

Where to put shared code for tests in a Go package? [duplicate]

This question already has answers here:
Can I create shared test utilities?
(2 answers)
Closed 3 years ago.
I have a Go package with multiple files. As of Go standard I am creating an associated test file for each source file in the package.
In my case the different tests use the same test helping functions. I don't want these functions to be in the package source files because it is only used for testing purpose. I also would like avoiding replicating this code in each test file.
Where should I put this code shared between all the test source files and not part of the package ?
You just put it in any of the test files and that's all. Test files using the same package clause belong to the same test package and can refer to each other's exported and unexported identifiers without any import statements.
Also note that you're not required to create a separate _test.go file for each of the .go files; and you can have an xx_test.go file without having a "matching" xx.go file in the package.
For example if you're writing package a, having the following files:
a/
a.go
b.go
a_test.go
b_test.go
For black-box testing you'd use the package a_test package clause in a_test.go and b_test.go. Having a func util() in file a_test.go, you can use it in b_test.go too.
If you're writing white-box testing, you'd use package a in the test files, and again, you can refer any identifiers declared in a_test.go from b_test.go (and vice versa) without any imports.
Note that however if the package clauses in a_test.go and b_test.go do not match (e.g. a_test.go uses package a and b_test.go uses package a_test), then they will belong to different test packages and then you can't use identifiers declared in one another.

Can buildr include the result of a subproject's package in the current project's resources?

If I want to package a subproject's jar inside the main jar, I can do this sort of thing:
define 'library' do
project.version = '0.1'
define 'subproject' do
package :jar
end
package(:jar).include(project('subproject').package(:jar),
as: 'org/acme/library/subproject.jar')
end
This will lazily build the jar in the subproject, right before it is needed for packaging into the main jar, and everything works.
Problem is, my tests want to use the jar file as well, so the logical place for it is in the resources. So I wonder how I'm supposed to copy something build by another project into the resources for this project. The docs are notably lacking any examples of this and mostly focus on how to copy one directory of files to another.
This is what I tried:
resources.from(project('subproject').package(:jar),
as: 'org/acme/library/subproject.jar')
It fails:
RuntimeError : Source directory $HOME/Documents/library/subproject/target/library-subproject-0.1.jar doesn't exist
$HOME/Documents/library/buildfile:38:in `block in <top (required)>'
To my surprise, this seems to be the one place in buildr which eagerly evaluates the existence of a build product instead of setting it up as a lazy dependency...
I can work around this as follows:
# Crappy workaround to eagerly create target dir in subproject.
mkdir_p project("lucene#{ver}").path_to(:target)
resources.from(project("lucene#{ver}").path_to(:target)).
include(project("lucene#{ver}").package(:jar))
I don't like this because it still eagerly evaluates the directory, which forces me to create that directory long before any of the build is being run. Meaning that even when I run buildr clean, it's creating this directory. Yuck.
So what's the proper way to do this?
The way we typically do this is to not create any packages with the top level project and instead use a subproject to define the two packages. i.e
define 'myproject'
...
define 'model' do
...
package(:jar)
end
define 'server' do
...
package(:war) do |war|
war.libs.clear
war.libs << project('model').package(:jar)
end
end
end
This allows much easier management of dependencies and ordering of builds. Hope that helps!

Where should I place my lib tests

Note that I have already read the layout convention.
In my lib directory I usually have a few libraries I could extract into their own package. Very often the code is not complete enough or / and I want to wait for a new package until I really want to reuse the code in another project.
I would really like to place the unit-test code, examples and doc in the same directory.
Example:
let's say I have a string-helper library in lib → lib/string-helper.
I would like to place my tests, examples and doc in lib/string-helper/tests, lib/string-helper/examplesand lib/string-helper/doc.
However the layout convention says that I should put them outside the lib directory.
This makes it unnecessarily hard to extract it into its own package. (pub serve even went into an endless loop when I ignored this and made my own package symbolic link)
How do you handle this?
The only valid place for tests is the my_package/test directory or any subdirectory of test.

Using Non-Local Data/Media Files with a C++ Application (gtkmm)

I'm beginning development on an acoustic spectrum analysis tool (inspired by spek) written in C++ with gtkmm (C++ bindings for the GTK+ GUI toolkit). I would imagine that I should know how to do this by now, however...
My directory structure is a-la-GNOME, e.g src/, data/, po/, man/. The specific situation that presented the need for my inquiry is the use of a GTK UI Manager that will be located in data/ui. For this specific situation, I want to be able to load the user-interface from this file in an install-independent manner (e.g. loading of the file does not depend on a make install; the executable may be run [and load the UI file] either from src/ after running make [thus compiling the sources into the selfsame exectuable] or from its install prefix). How would I refer to the UI file in my source code (keeping in mind that the loading of the file is not performed by creating a file object (fopen(...)) but rather by passing a file location as a string argument to (UIManager).add_ui_from_file(...))?
In addition to this particular situation of a UI file, how would I do similar references to files (i.e. databases, INI files, XML schemas) by using the autotools build process? Is there a piece of relevant Automake code to quickly set up a project to use this type of directory structure?
simply try to use both files (with the un-installed taking precedence):
if(!(UIManager).add_ui_from_file(../data/ui/mygui))
(UIManager).add_ui_from_file(/incalled/location/mygui)
In Glom, I created a helper function that tries both locations, with both locations being defined in the Makefile.am (this is simpler if you have only one Makefile.am, by using non-recursive automake, which is simpler anyway):
http://git.gnome.org/browse/glom/tree/glom/glade_utils.h#n38