In this README I give instructions for a quick CL for testing the released tool. I think it would be much better if I provided a .bat and unix script which executed the commands in one click/command. At the same time, unlike a compiled program, it's transparent and users can open the script with the editor and inspect the commands executed.
Can I in a bat save a file?
This is what I'd like it to execute.
$ vim Test.java (windows: notepad Test.java)
class T {
private static void p(int i, Double d, String... s){}
}
public class Test{
#com.dp4j.InjectReflection public void t() {
T.p(1,new Double(2),"hello", "reflection");
}
}
$ ls Test.class T.class (windows: dir Test.class T.class)
ls: Test.class: No such file or directory ls: T.class: No such file or directory
$ javac -cp dp4j-1.0-jar-with-dependencies.jar Test.java
$ ls Test.class T.class (windows: dir Test.class T.class)
ls Test.class T.class
Yes.
You can put each line in an echo command piped to the file:
echo class T { > MyFile.java
echo private static void p(int i, Double d, String... s){} >> MyFile.java
echo } >> MyFile.java
echo ... >> MyFile.java
> creates a file; >> appends to it.
You can then compile it normally.
Um, perhaps this?
// 2>NUL&GOTO :START
//Just ignore the above line, it's for the batch script.
class T {
private static void p(int i, Double d, String... s){}
}
public class Test{
#com.dp4j.InjectReflection public void t() {
T.p(1,new Double(2),"hello", "reflection");
}
}
/*We start the batch script here.
:START
#CLS&ECHO OFF
START NOTEPAD %0
IF EXIST Test.class GOTO :EXISTS
IF EXIST T.class GOTO :EXISTS
JAVAC -cp dp4j-1.0-jar-with-dependencies.jar %0
GOTO :END
:EXISTS
ECHO There is a preexisting class file. Aborting.
:END
REM We end the batch script here.*/
By the way, here the batch script and java source are the same file.
This is a unix exectable script that does what I want. I don't know if it works with cygwin on windows:
#!/bin/sh
v=1.1
test_file="Test10.java"
jar_file="dp4j-$v-jar-with-dependencies.jar"
cmd="curl -O --fail -L http://downloads.sourceforge.net/project/dp4j/$v/$jar_file"
echo $cmd
$cmd
echo
# Start
cat > $test_file << __EOF__
class T10 {
private static void p(int i, Double d, String... s){}
}
public class Test10{
#com.dp4j.InjectReflection
public void t() {
T10.p(1,new Double(2),"hello", "reflection");
}
}
__EOF__
cmd="cat $test_file"
echo $cmd
$cmd
echo
cmd="javac -Averbose=true -cp $jar_file $test_file"
echo $cmd
$cmd
echo
echo "TEST PASSED: $test_file was compiled with Reflection Injected."
echo "When JUnit/TestNG.jar is in the classpath you may use #Test in lieu of #InjectReflection."
echo "Javadoc, sources, and other artifacts maybe downloaded from http://repo2.maven.org/maven2/com/dp4j/dp4j/"$v"/"
Here's one line command to download it and execute it:
wget http://sourceforge.net/projects/dp4j/files/1.2/TESTDRIVE ; chmod +x TESTDRIVE ; ./TESTDRIVE
Related
I use Jenkinsefile file to run the Stages.
It is in Jenkins pipeline installed on windows, Declarative pipeline.
On the begining I do:
pipeline {
agent { label 'master'}
environment {
My_build_result = 7
}
....
Than
stage('Test') {
steps {
echo 'Testing..'
bat """
cd Utils
"C:\\Program Files\\MATLAB\\R2019b\\bin\\matlab.exe" -wait -nodisplay -nosplash -nodesktop -r "run('automatic_tests\\run_test.m');"
echo %errorlevel%
set /a My_build_result_temp = %errorlevel%
set My_build_result = %My_build_result_temp%
"""
script {
My_build_result = bat(returnStatus:true , script: "exit (2)").trim()
echo "My_build_result ${env.My_build_result}"
if (My_build_result != 0) {
echo "inside if"
}
}
}
}
The variable My_build_result get value 7 at the begining
Inside the bat section, it suppose to get value 0 from %errorlevel%
Inside the script section it suppose to get value 2
BUT
in the echo "My_build_result ${env.My_build_result}" I get print of 7
(and it goes inside the if sentense)
How do I define variable that can be set value in bat"""
"""
and in script """
"""
section of the stage
and also be familiar in another stages and in the post { always { .. }} at the end ???
BTW: add env.before My_build_result (env.My_build_result ) does not work
Thanks a lot
In the first bat call, you are setting the environment variable only inside of the batch script environment. Environment variable values that are assigned through set don't persist when the script ends. Think of these like local variables. Simply use returnStatus: true to return the last value of ERRORLEVEL. There is no need to use %ERRORLEVEL% in the batch script here.
steps {
script {
My_build_result = bat returnStatus: true, script: """
cd Utils
"C:\\Program Files\\MATLAB\\R2019b\\bin\\matlab.exe" -wait -nodisplay -nosplash -nodesktop -r "run('automatic_tests\\run_test.m');"
"""
// My_build_result now has the value of ERRORLEVEL from the last command
// called in the batch script.
}
}
In the 2nd bat call the 1st mistake is to call the trim() method. Result type of bat step is Integer, when returnStatus: true is passed. The trim() method is only available when returnStdout: true is passed in which case the result type would be String. The 2nd mistake is to use brackets around the exit code value. The fixed code should look like:
My_build_result = bat returnStatus: true, script: "exit 2"
// My_build_result now equals 2
I have a pipeline in which I am trying to pass the Build Number.
When I print the Build Number its prints fine however, I am having issues passing it to the a shell ansible command inside a stage.
pipeline {
agent { label 'Prod_Slave' }
stages {
stage('Ansible stuff') {
parallel {
stage('APP') {
steps {
echo "${env.BUILD_NUMBER}"
println "${env.BUILD_NUMBER}"
sh 'ansible-playbook $ansible_playbook/site.yml -e Latest_Build_Number4=${env.BUILD_NUMBER}'
}
}
}
}
}
}
Output:
[Pipeline]
echo (hide)
16 [Pipeline]
echo 16 [Pipeline]
echo 16
/script.sh: line 1: Latest_Build_Number4=${env.BUILD_NUMBER}: bad
substitution
When i change to:
sh 'ansible-playbook $ansible_playbook/site.yml -e Latest_Build_Number4="${env.BUILD_NUMBER}"'
Output:
#2#tmp/durable-5ed077ca/script.sh: line 1: ${env.BUILD_NUMBER}: bad
substitution
Finally tried this:
sh 'ansible-playbook $ansible_playbook/site.yml -e Latest_Build_Number4="$env.BUILD_NUMBER"'
Output:
-e Latest_Build_Number4=.BUILD_NUMBER
Single quotes won't allow substitution. Try double quotes, escaping the dollar sign:
sh "ansible-playbook \$ansible_playbook/site.yml -e Latest_Build_Number4=${env.BUILD_NUMBER}"
I am attempting to understand how to structure a go project into sub modules stored in separate source code repositories (on host example.com), however when I do, I am not sure how to run the tests that are within a module. What am doing wrong in the following example, any help is much appreciated!!
mkdir -p src/example.com/john/tool
echo "package tool" >> src/example.com/john/tool/book.go
echo "" >> src/example.com/john/tool/book.go
echo "type Book struct {" >> src/example.com/john/tool/book.go
echo " Title string" >> src/example.com/john/tool/book.go
echo "}" >> src/example.com/john/tool/book.go
echo "" >> src/example.com/john/tool/book.go
echo "package tool" >> src/example.com/john/tool/book_test.go
echo "" >> src/example.com/john/tool/book_test.go
echo "import (" >> src/example.com/john/tool/book_test.go
echo " \"tool\"" >> src/example.com/john/tool/book_test.go
echo " \"testing\"" >> src/example.com/john/tool/book_test.go
echo ")" >> src/example.com/john/tool/book_test.go
echo "" >> src/example.com/john/tool/book_test.go
echo "func TestBook(t *testing.T) { }" >> src/example.com/john/tool/book_test.go
echo "" >> src/example.com/john/tool/book_test.go
export GOPATH=`pwd`
go test example.com/john/tool
When I run this test, this is the error I am seeing:
# example.com/john/tool
src/example.com/john/tool/book_test.go:4:3: cannot find package "tool" in any of:
/usr/local/go/src/tool (from $GOROOT)
/Users/john/app/src/tool (from $GOPATH)
FAIL example.com/john/tool [setup failed]
Obviously book_test.go can't import the "tool" package, an you could probably put in the full path, but when I look in github, no one does that in their modules. So I don't understand what I am doing wrong.
Your line
import "tool"
is the offender. There is no package tool in the standard library and your package tool has an import path of example.com/john/tool.
Just drop that import. There is no need to import the current package (and it is impossible as this would be a (degenerated) import cycle).
Say, I have a C code which I compile like:
$ gcc code.c -o f.out
$ ./f.out inputfile outputfile
Then the code asks for input
$ enter mass:
Now if I need to run this code for example 200 times and the input files have name : 0c.txt, 1c.txt, ....., 199c.txt etc and I want to use same value of mass every time (e.g. mass=6) then how do I write an "awk" command for that? Thanks for your help.
You don't specify your outputfile name. I'll assume 0c.out, 1c.out, ...
I'm also assuming that the f.out program reads the mass from stdin instead of anything more complicated.
#!/usr/bin/gawk -f
BEGIN {
mass = 6
for (i=0; i<200; i++) {
cmd = sprintf("./f.out %dc.txt %dc.out", i, i)
print mass |& cmd
close(cmd, "to")
while ((cmd |& getline out) > 0) {
do something with each line of output from ./f.out
}
close(cmd)
}
}
ref http://www.gnu.org/software/gawk/manual/html_node/Two_002dway-I_002fO.html
In bash, you'd write:
for i in $(seq 0 199); do
echo 6 | ./f.out ${i}c.txt ${i}c.out
done
I have now whittled this down to a minimal test case. Thus far I have been able to determine that this is an issue related to pseudo-terminals which come about with the pipe of ssh. Adding the '-t -t' to the ssh call improved things, in that now, it takes a second call to fgets() to cause the issue. I suspect that the stderr output of the ssh command somehow works into the issue, for now I have redirected stderr to stdout in the ssh code to execute. I do wonder if the "tcgetattr: Invalid argument" error is part of the problem, but am not sure how to get rid of that. It seems to come from the -t -t being present. I believe the -t -t is moving in the right direction, but I have to set up the pseudo terminal for stderr somehow and perhaps the test will work properly?
The Makefile:
test:
gcc -g -DBUILD_MACHINE='"$(shell hostname)"' -c -o test.o test.c
gcc -g -o test test.o
.PHONY: clean
clean:
rm -rf test.o test
The test.c source file:
#include <unistd.h>
#include <string.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
const unsigned int bufSize = 32;
char buf1[bufSize];
char buf2[bufSize];
int ssh = argv[1][0] == 'y';
const char *cmd = ssh ? "ssh -t -t " BUILD_MACHINE " \"ls\" 2>&1" : "ls";
FILE *fPtr = popen(cmd, "r");
if (fPtr == NULL) {
fprintf(stderr,"Unable to spawn command.\n");
perror("popen(3)");
exit(1);
}
printf("Command: %s\n", cmd);
if (feof(fPtr) == 0 && fgets(buf2, bufSize, fPtr) != NULL) {
printf("First result: %s\n", buf2);
if (feof(fPtr) == 0 && fgets(buf2, bufSize, fPtr) != NULL) {
printf("Second result: %s\n", buf2);
int nRead = read(fileno(stdin), buf1, bufSize);
if (nRead == 0) {
printf("???? popen() of ssh consumed the beginning of stdin ????\n");
} else if (nRead > 0) {
if (strncmp("The quick brown fox jumped", buf1, 26) != 0) {
printf("??? Failed ???\n");
} else {
printf("!!!!!!! Without ssh popen() did not consume stdin !!!!!!!\n");
}
}
}
}
}
This shows it running the passing way:
> echo "The quick brown fox jumped" | ./test n
Command: ls
First result: ARCH.linux_26_i86
Second result: Makefile
!!!!!!! Without ssh popen() did not consume stdin !!!!!!!
This shows it running the failing way:
> echo "The quick brown fox jumped" | ./test y
Command: ssh -t -t hostname "ls" 2>&1
First result: tcgetattr: Invalid argument
Second result: %backup%~ gmon.out
???? popen() of ssh consumed the beginning of stdin ????
Okay, I have got this working finally. The secret was to supply /dev/null as the input to my ssh command as follows from the test case above:
const char *cmd
= ssh ? "ssh -t -t " BUILD_MACHINE " \"ls\" 2>&1 < /dev/null" : "ls";
However, while the code works correctly, I get a nasty message which apparently I can ignore for my purposes (although I'd like to make the message go away):
tcgetattr: Inappropriate ioctl for device