go unit test runs from %APPDATA% - unit-testing

I am trying to run some of my Go unit tests using "go test" but the test executable is built and run from my machine's %APPDATA%/local/temp directory. My PC has IT enforcement which blocks any unrecognized executable from being run other than from a pre-sanctioned directory (i.e C:/dev/projects"). All my Go source code are in that directory, including my *_test.go files. Is there a way to tell the Go test module to build and run from the current directory?

Yes you can.
Setting temp directory before executing the go test. By default temp directory environment variable gets evaluated in the order of TMP, TEMP, USERPROFILE, Windows directory; refer to msdn doc.
Basically it complies the go test under given temp directory and execute it.
C:\> cd dev\projects\src\mygotest
C:\dev\projects\src\mygotest>echo %CD%
C:\dev\projects\src\mygotest
C:\dev\projects\src\mygotest>set TMP=%CD%
C:\dev\projects\src\mygotest>go test -x
WORK=C:\dev\projects\src\mygotest\go-build306298926
mkdir -p $WORK\mygotest\_test\
mkdir -p $WORK\mygotest\_test\_obj_test\
cd C:\dev\projects\src\mygotest
"C:\\Go\\pkg\\tool\\windows_amd64\\compile.exe" -o "C:\\dev\\projects\\src\\mygotest\\go-build306298926\\mygotest\\_test\\mygotest.a" -trimpath "C:\\dev\\projects\\src\\mygotest\\go-build306298926" -p main -complete -buildid 86cb7a423d355c7468ad98c4f8bffe77b68d2265 -D _/C_/dev/projects/src/mygotest -I "C:\\dev\\projects\\src\\mygotest\\go-build306298926" -pack "C:\\dev\\projects\\src\\mygotest\\sample.go" "C:\\dev\\projects\\src\\mygotest\\sample_test.go"
cd $WORK\mygotest\_test
"C:\\Go\\pkg\\tool\\windows_amd64\\compile.exe" -o "C:\\dev\\projects\\src\\mygotest\\go-build306298926\\mygotest\\_test\\main.a" -trimpath "C:\\dev\\projects\\src\\mygotest\\go-build306298926" -p main -complete -D "" -I "C:\\dev\\projects\\src\\mygotest\\go-build306298926\\mygotest\\_test" -I "C:\\dev\\projects\\src\\mygotest\\go-build306298926" -pack "C:\\dev\\projects\\src\\mygotest\\go-build306298926\\mygotest\\_test\\_testmain.go"
cd .
"C:\\Go\\pkg\\tool\\windows_amd64\\link.exe" -o "C:\\dev\\projects\\src\\mygotest\\go-build306298926\\mygotest\\_test\\mygotest.test.exe" -L "C:\\dev\\projects\\src\\mygotest\\go-build306298926\\mygotest\\_test" -L "C:\\dev\\projects\\src\\mygotest\\go-build306298926" -w -extld=gcc -buildmode=exe "C:\\dev\\projects\\src\\mygotest\\go-build306298926\\mygotest\\_test\\main.a"
$WORK\mygotest\_test\mygotest.test.exe
Hello, playground
PASS
ok mygotest 0.526s
C:\dev\projects\src\mygotest>
Note: TMP set to current terminal session only, it doesn't affect system environment variable.
Important thing to note from above test output is WORK=C:\dev\projects\src\mygotest\go-build306298926.
Happy testing!

Related

Cpp in VSCode - How to compile to other folder

I'm using VSCode to program in C++.
When I run the code (using the "Code Runner" extension), the .exe file stored in the same folder.
I've seen in many projects that they stored it in a /bin folder.
Is it posibble to automatically store it in there? (Also create the /bin folder if it doesn't exists).
Thank you!
I figured out 2 ways how to do your task:
In this scenario you have to create bin folder manually
So let's configure your VScode extension
From extension page follow this step to open settings
From extension page follow this step to open settings
In settings tab open "Edit in settings.json"
In your Json find "code-runner.executorMap"::cpp and insert the following command instead of the existing one
cd $dir/bin && g++ ../$fileName -o $fileNameWithoutExt && $fileNameWithoutExt
So it would look like this
P. S. This command works on Windows, and may not work on linux. I guess If the previous command does not work in linux, then try the next one
cd $dir/bin && g++ ../$fileName -o $fileNameWithoutExt && ./$fileNameWithoutExt
And I think, if you're linux user you can automate everything with a command(but unfortunately I can't test it)
cd $dir && mkdir -p bin && cd bin && g++ ../$fileName -o $fileNameWithoutExt && ./$fileNameWithoutExt
In this scenario you have to switch execution to integrated terminal(so output will change a little bit) by adding to your json following line
"code-runner.runInTerminal": true
and changing your cpp command to:
for windows
cd $dir && mkdir -Force bin > $null && cd bin && g++ ../$fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt
and I guess for linux
cd $dir && mkdir -p bin && cd bin && g++ ../$fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt
So it would looks like this
For those who use a /src and /bin separately, here's my solution:
change "cpp": under "code-runner.executorMap": to
"cpp": "cd $dir && g++ $fileName -o ..\\bin\\$fileNameWithoutExt && ..\\bin\\$fileNameWithoutExt",
This creates and runs the .exe from the bin folder within the parent.
If anbody is interested in separating executables to a \bin directory and running the code with external cmd.exe use this (code-runner.executorMap):
"c": "cd $dir && gcc $fileName -o .\\bin\\$fileNameWithoutExt && start cmd \"/k ; .\\bin\\$fileNameWithoutExt\""
This is for C but is easily modified for C++.
To add to Deumaudit's answer for Linux users,
Create a bin folder in your working directory manually.
Now in your settings.json file under "code-runner.executorMap"::cpp insert the following command instead of the existing one:
cd $dir && g++ $fileName -o $dir/bin/$fileNameWithoutExt && $dir/bin/$fileNameWithoutExt

CentOS7: rpmbuild - Unable to recognise the format of the input file

I'm trying to build an extremely simple rpm over centos7.
I just copy some pre-compiled executables from the tar.gz to /usr/bin/my_rpms/rpm1.
Here is my install section:
%install
mkdir -p %{buildroot}/usr/bin/my_rpms/rpm1/
install -D prog prog.o -t %{buildroot}/usr/bin/my_rpms/rpm1/
it used to work find for the most part.
but today when after i made some changes to the prog and re-compiled it keeps gettings these errors:
+ mkdir -p /root/rpmbuild/BUILDROOT/rpm1.x86_64/usr/bin/my_rpms/rpm1/
+ install -D prog prog.o -t /root/rpmbuild/BUILDROOT/rpm1.x86_64/usr/bin/my_rpms/rpm1/
+ /usr/lib/rpm/check-buildroot
+ /usr/lib/rpm/redhat/brp-compress
+ /usr/lib/rpm/redhat/brp-strip /usr/bin/strip
/usr/bin/strip: Unable to recognise the format of the input file `/root/rpmbuild/BUILDROOT/rpm1.x86_64/usr/bin/drivertest_rpms/rpm1/prog.o'
As you can see in error log, problem is with binary file striping which is default behavior of install command. I think your build environment is maybe different then rpm environment. cross-compiling? as suggested by #aaron-d-marasco
So I recommend to build rpm from project source. i.e move your build commands into %build section of .spec file.
Or strip your files in the same place where you have build them, and then in rpm use cp command in %install section instead of install command to move your files to target directory.

How can I create a dockerfile for .deb package containing a c++ source file?

C++ file with name first.cpp
#include<iostream>
using namespace std;
int main(){
cout << "Hello world..!!";
return 0;
}
Below is my dockerfile..
from debian:latest
LABEL:first
LABEL DESCRIPTION:Print Hello world on the screen
MAINTAINER saurav <sauravgore97#gmail.com>
COPY . /var/www/deb
WORKDIR /var/www/deb
RUN ./first
CMD ["./first"]
I tried with above dockerfile but I keep failing..gives me the error that
the directory "deb" does not exist. In fact it is in the same directory as the dockerfile where I build.
In order for the cpp file to run, it needs to be compiled first, for which we need a compiler. Since the g++ doesn't come preinstalled on debian:stretch (which is what I used), it needs to be installed first.
FROM debian:stretch
# Set the working directory
WORKDIR /tmp
# Install the compiler
RUN apt-get update && apt-get install g++ -y
# Copy the file containing the source code to WORKDIR/first.cpp
COPY first.cpp first.cpp
# Compile the program
RUN g++ first.cpp -o first
# Set the compiled program as the main command of the container
CMD ["./first"]
Build it using:
docker build -f Dockerfile . -t=first-cpp
And run it using:
docker run -ti first-cpp
This will in turn run the container and just print out:
Hello world..!!

Eclipse not calling sub-makefiles correctly

I am trying to refactor a single makefile project to hierarchical structure. The project is imported in Eclipse as "External C/C++ project with makefile".
The new folder with the separate makefile contains source files and a makefile with the following recipe:
.PHONY: test
test:
echo "test"
The top directory contains a the top-level makefile with the following recipe:
clean:
# echo ...cleaning
cd CppAudioPeriphs && make test
rm -f $(OBJECTS) $(NAME).lst $(NAME).elf $(NAME).bin $(NAME).s19 $(NAME).map $(NAME).dmp
When I call from Eclipse Clean project, the last line from the last recipie clearly completes correctly. However, the line, asking to go to the sub-directory and execute make clean returns with the following message:
make[1]: `build/PeriphPhysical.o' is up to date.
This is the first object file declared, and the message is the same even if recipe test does not exist.
On the other hand, from the command line, everything works. Id est open cmd.exe, go to project folder, type make clean - > the "echo test" command gets executed.
I am using gcc and binutils, compiled for Windows, for cross-compilation for arm. Where could be my problem.
EDIT: response to jimmy
These may be additional clues.
1) If I replace
cd CppAudioPeriphs && "make test"
with
cd CppAudioPeriphs && C:\arm_tools\tools\bin\make.exe test
, the result is:
/usr/bin/sh: C:arm_toolstoolsbinmake.exe: command not found
If I change the slashes to forward slashes, the old message of ``build/PeriphPhysical.o' is up to date.` pops back in.
Replaced
cd CppAudioPeriphs && C:\arm_tools\tools\bin\make.exe test
with
make -C CppAudioPeriphs test
as a workaround and now everything compiles.

How to build nodejs as a shared library from source code

I need to include node.h in my c++ project, I tried to build node from source code using:
./configure
sudo make
I got a node executable and some object files and .a files, I need to build as .so file to use it in my c++ code.
I tried to build libnode, but I got cmakelists error and this is not official nodejs project.
if anybody know how to build nodejs from source code as .so file will be great, a similar question in a google group but the answer is not working.
Support for building as a shared library has been added in to node mainline. Please see PR 6994 and specifically this comment.
I just ran
git clone https://github.com/nodejs/node.git
cd node
git checkout v6.9.4
./configure --shared
make -j4
which produced:
ubuntu#server:~/node$ find . -name libnode.so\* -exec ls -la {} \;
-rwxrwxr-x 2 ubuntu ubuntu 31576776 Jan 6 18:57 ./out/Release/lib.target/libnode.so.48
-rw-rw-r-- 1 ubuntu ubuntu 387 Jan 6 18:57 ./out/Release/.deps/home/ubuntu/node/out/Release/lib.target/libnode.so.48.d
-rw-rw-r-- 1 ubuntu ubuntu 4202 Jan 6 18:57 ./out/Release/.deps/home/ubuntu/node/out/Release/obj.target/libnode.so.48.d
-rwxrwxr-x 2 ubuntu ubuntu 31576776 Jan 6 18:57 ./out/Release/obj.target/libnode.so.48
ubuntu#server:~/node$
I think it is easier to build in a static library as shared requires the addition of '-fpic'.
For my projects (under Linux) I use this script to built a static node.js library:
#!/bin/sh
# This script is LGPL feel free to use it!
if test ! "$#" = "1"; then
echo "Run with the archive in parameter:"
echo "\t${0} ./node-v0.XX.XX.tar.gz"
echo "\nIt will build a ./libnode_static.a in current dir"
return
fi
HERE=$PWD
#Extract Tarball
tar xf $1 | exit 1
DIRNAME=`echo $1 | sed s/.tar.gz//g`
cd $DIRNAME
#Patch node.gyp to build in static
sed -i "s/'type': 'executable',/'type': 'static_library',/g" ./node.gyp
#Patch node_main.cc to rename the main in node_main
sed -i "s/int main(/int node_main(/g" ./src/node_main.cc
#Build Node.js
./configure
make -j8
#Move to build directory
cd ./out/Release
#Extract .a
#Cleanup if previous build
rm -fr *.tmpd
echo "== Extracting *.a =="
#Make sure we create a directory
#for each.a as some .o might
#have the same name
for a in `ls *.a`
do
echo "\t${a}..."
mkdir "$a.tmpd"
cd "$a.tmpd"
ar x ../$a
cd ..
done
#Repack in a single .a
find . -iname "*.o" | xargs ar rcs libnode_static.a
#Cleanup
rm -fr *.tmpd
echo "== DONE =="
#Move in start directory
mv ./libnode_static.a ${HERE}/
cd ${HERE}
#Sanity CHECK
echo "== Performing Sanity Check =="
TMP_FILE=`mktemp /tmp/XXXXXX.cxx`
TMP_EXE=`mktemp /tmp/XXXXXX`
cat << . > ${TMP_FILE}
int node_main( int argc, char **argv);
int main(int argc, char ** argv )
{
node_main( argc, argv );
return 0;
}
.
#Try compiling
g++ ${TMP_FILE} -o ${TMP_EXE} -lnode_static -ldl -pthread -L.
#Try running
RET=`${TMP_EXE} -e "console.log('okfromnode')"`
if test "x${RET}" = "xokfromnode"; then
echo "== Sanity check OK =="
else
echo "== Sanity check FAILED =="
exit 1
fi
rm ${TMP_FILE} ${TMP_EXE}
echo "== Node.js is now built statically in ./libnode_static.a =="
exit 0
Run it as follows :
sh script.sh node-v0.10.XX.tar.gz
If everything goes well you should get a libnode_static.a in current directory.
Use it with a code like this:
int node_main( int argc, char **argv);
int main(int argc, char ** argv )
{
/* Here we spawn a node.js instance */
return node_main( argc, argv );
}
And compile like this:
g++ ./test.cxx -o ./my_node -lnode_static -ldl -pthread -L.
And you have embedded node :
./my_node -e "console.log('Hello World')"
#Outputs
Hello World
Hope this helps.
This is how i did it in windows. Except for the build procedures, everything should be same.
Nodejs uses node-gyp for building. You can read this for building and installation. Or just git clone the repository.
Open node.gyp in the node-vX.XX.XX and find
'targets': [
{
'target_name': 'node',
'type': 'executable',
change the executable to shared_library.
Run vcbuild.bat in windows or for other platforms follow instructions.
Update:
https://gist.github.com/aklen/849f3460b7a028c9aed8a84e1d4cecb7
Windows
.\vcbuild release vs2017 dll x64
.\vcbuild release vs2017 dll x86
.\vcbuild debug vs2017 dll x64
.\vcbuild debug vs2017 dll x86
Linux / MacOS
./configure --shared --release
make -j4
./configure --shared --debug
make -j4
Other build options
https://github.com/nodejs/node/blob/master/BUILDING.md