go not running program with name package_test.go - unit-testing

I have following code under package pack1. Name of file is pack1.go
package pack1
var Pack1Int int = 42
var pack1Float = 3.14
func ReturnStr() string {
return "Hello world!"
}
And following code in main program. Name of file is package_test.go
package main
import (
"fmt"
"./pack1"
)
func main() {
var test1 string
test1 = pack1.ReturnStr()
fmt.Printf("Return string from pack1 : %s\n", test1)
fmt.Printf("Integer from pack1 : %d\n", pack1.Pack1Int)
}
When I try to run it with command go run package_test.go I get following error:
go run: cannot run *_test.go files (package_test.go)
But if I rename file to abc.go then I am getting proper output i.e.
Return string from pack1 : Hello world!
Integer from pack1 : 42
I am curious about what is wrong with using package_test.go as file name.
For code with only main package this name is working fine.
Is this a bug in Go or I am doing something wrong ?

Not a bug, it's designed so. go run detects the _test files and consider them as test files for a package, test files will be compiled as a separate package, and then linked and run with the main test binary.
It's recommended to put your package file to GOPATH/src/PACK_NAME/, then run your *_test.go with go test.

You can't name your program files as *_test.go as this is part of integrated Go testing system
To write a new test suite, create a file whose name ends _test.go that contains the TestXxx functions as described here. Put the file in the same package as the one being tested. The file will be excluded from regular package builds but will be included when the “go test” command is run. For more detail, run “go help test” and “go help testflag”.
Just rename package_test.go to packagetest.go

For BASH
Run go run PATH_TO_FILES/!(*_test).go
NOTE
If you get an event not found error when running this command, you probably need to enable extended globbing in your bash terminal.
Run shopt-s extglob
after you Run go run PATH_TO_FILES/!(*_test).go
For those using ZSH
setopt extendedglob # to get help regarding globbing
next
foo*~*bar* # match everything that starts with foo but doesn't contain bar
So with my case, go run PATH_TO_FILES/*~*_test.go*

if you are using linux:
go run `ls PATH_TO_FILES/*.go | grep -v _test.go`
it workes for me.

Related

Visual Studio cross platform makefile project, command not found

I'm trying to build a cross platform project for Ubuntu. In my makefile I have the line
"PSPSDK=$(shell psp-config --pspsdk-path)"
which gives the error "psp-config: Command not found."
psp-config is in my path and running make from the Ubuntu system on the files that get copied over from Visual Studio works fine. It also works if I manually ssh into the Ubuntu system from windows and run the command from there.
Why can't it find the command when run through Visual Studio?
You should update PATH at the beginning of "~/.bashrc" file (and not at the end) because it starts with somethign like:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
Also, if you add code before these lines, it will be invoked for every subshell execution, so it is better to add a guard for it as well so it is invoked only once per session:
if [ -z $HOME_OPT_PATH_SET ]; then
export PATH=$PATH:$HOME/opt
export HOME_OPT_PATH_SET=1
fi
# If not running interactively, don't do anything
...

C++ executable, sh 1:not found

I created a c++ programm that works with ros. The first step would be to open a roscore in a terminal and move on from there. I do so with system("roscore &");
I compiled my file and can run it just fine with ./file.
However, I want to be able to run it as an application (double click). I created a .desktop file and the program shows up in my application list. When i start it though, all I get is a terminal that opens with the message
sh: 1: roscore: not found
etc.
The same applies for the roslaunch commands. I also fork and exec a roslaunch command, which does not work as well.
I tried system("ls"); which worked. All cout messages work as well.
Any idea what is wrong here?
roscore executable is not located in std paths (/bin:/usr/bin:). Use the absolute path - system("/path/to/roscore &")

Executing a cmd using "system()" doesn't look for path

I'm trying to execute a graphviz/dot cmd from within my program,
but I'm still getting the error sh: dot: command not found.
So that's the line that matters:
system("dot -Tpng myfile.txt -o mypic.png");
Executing it on the terminal works pretty well, cause dot is set into my path variable.
Trying to execute the same programmatically ends up in the described error.
Is there any trick using the system cmd where I have to mention my path additionally?
(Btw: I already did include stdlib.h, dunno wether that's important...)
Thx a lot!
You can try something like this:
#include <stdlib.h>
int main()
{
system("PATH=$PATH:<YOUR PATH TO **dot**>;<**dot** and the rest>");
}
as shocking as it seems, this works for me ...
(obviously replace between < > ... )

PyInstaller not working on simple HelloWorld Program

So I am running on 64-bit Windows 7, and I set up Pyinstaller with Pip and PyWin32. I have python 2.7.
I made a simple hello world Program with this code
print "hello world!"
I put the file in the same directory as PyInstaller, and ran this code in the command prompt
pyinstaller.py helloWorld.py
Yet, when I try that, I get this error message:
Error loading Python DLL: C:\PROGRA~1\PYINST~1.1\build\HELLOW~1\python27.dll (error code 126)
What am I doing wrong and how do I fix this?
Run with the -F flag to produce the standalone exe:
pyinstaller -F helloworld.py
It will output to dist/helloworld.exe
NOTE this is a different location to when -F is not used, be sure to run the right exe afterwards.
Thanks #tul! My version of pyinstaller put it to dist\helloworld.exe though!
If you start it from C:\Python27\Scripts... that'll be C:\Python27\Scripts\dist... as well!
But whereever you have it, I recommend putting a batch file next to your .py to be able recompile any time with just a click:
I set it up so there is nothing but the .exe at the .py location and the temporary stuff goes to the temp dir:
#echo off
:: get name from filename without path and ext
set name=%~n0
echo ========= %name% =========
:: cut away the suffix "_build"
set name=%name:~0,-6%
set pypath=C:\Python27\Scripts
set buildpath=%temp%
if not exist %name%.py (
echo ERROR: "%name%.py" does not exist here!
pause
exit /b
)
%pypath%\pyinstaller.exe --onefile -y %~dp0%name%.py --distpath=%~dp0 --workpath=%buildpath% --specpath=%buildpath%
I name it like the .py file plus "_build" and cut away the suffix in the batch script again.
Voilà.

C++ program cannot find latex command with system() function call

I am writing a C++ program which has to automatically generate some data to be used by students in an integrated exercise. I have already exported this data to .tex files and would also like the C++ program to be able to compile these tex files automatically.
Usually I would compile tex files from the command line by doing the following:
$ latex file.tex
$ latex file.tex
$ dvipdf file.dvi
So I tried doing the following in my C++ code (directory and filename are both strings):
//Move to the location where I created the files
string mycommand = "cd ";
mycommand += directory;
system(mycommand.c_str());
//Compile latex
mycommand = "latex " + filename + "_main.tex";
system(mycommand.c_str());
system(mycommand.c_str());
//Create .pdf
mycommand = "dvipdf " + filename + "_main.dvi";
system(mycommand.c_str());
Which then produces the following error message on the terminal output:
sh: latex: command not found
sh: latex: command not found
sh: dvipdf: command not found
I have searched this online but I have failed to find a solution for this problem, though I believe it is likely to be something very simple.
I am working on OSX and have the following version of latex installed:
pdfTeX 3.1415926-2.4-1.40.13 (TeX Live 2012)
kpathsea version 6.1.0
All help is greatly appreciated!
First, the path to the programs latex and dvipdf needs to be in your PATH environment variable.
Second, the calls of the shell via system are totally independent (in fact a new instance of the shell is started each time). So if you switch the directory in one, this does not affect the others. Switch instead the current directory of your program via:
chdir(directory.c_str())
You need for this
#include <cunistd>
using namespace std;
at the beginning of the file.
Please note that system calls with command lines depending on input parameters can be easily exploited to run arbitrary commands if the parameters (in your case the filename) are not carefully checked. As you do not have quotes, the program fails if there are e.g. whitespaces in the filename.