How do you structure imports for testing inside a module of your project? - testing

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).

Related

Calling gammu or gammu-smsd-inject

I have gammu-smsd up and running on a raspberry-pi with jessie. I am using runonreceive to process incoming texts. I have the following script working using runonreceive. In the script I am calling gammu sendsms instead of gammu-smsd-inject as the documentation states. All other references state gammu will not work while gammu-smsd daemon is running. The only reason I got this to work is after pulling my hair out trying to get gammu-smsd-inject to work. Can anyone explain what is going on?
RunOnReceive = /home/jaalfs/bin/sms_back.sh
sms_back.sh
#!/bin/bash
from=$SMS_1_NUMBER
echo "sms_back" >> /home/jaalfs/bin/sms_back.log
echo "Test from: $from" >> /home/jaalfs/bin/sms_back.log
echo -e "\n"
if [ "$from" != "+1310xxxxxxx" ]; then
echo -e "not accepted number \n" >> /home/jaalfs/bin/sms_back.log
exit 0
else
echo "accepted number" >> /home/jaalfs/bin/sms_back.log
echo "hello world!!!!!!" | sudo gammu sendsms TEXT "$from"
echo -e " text sent back \n" >> /home/jaalfs/bin/sms_back.log
exit 0
fi
exit 1

String comparison in ksh never succeeding

I have the next script, and when trying to compare variable value if equals "NO" or "SI" (yes in spanish) it's not working for some reason I keep going all the time through the else (SI) although the real value in the variable is "NO". It's even being printed in the email subject.
I fear I could be some extra invisible character I can't see but it's there?
Here is the script:
#!/usr/bin/ksh
VAR=$(/home/userName/scripts/loadedresource.ksh | egrep 'SI|NO')
MAIL_FILE="testfile.txt"
rm -f $MAIL_FILE
echo "From:Script" > $MAIL_FILE
echo "To:Me<me#company.com>" >> $MAIL_FILE
echo "Subject:RESOURCE LOADED-> $VAR" >> $MAIL_FILE
echo "Content-Type: text/html" >> $MAIL_FILE
echo "<html>" >> $MAIL_FILE
echo "<body style='font-family:calibri;font-size:14px;'>" >> $MAIL_FILE
if [ "$VAR" == "NO" ]
then
echo "<h2> Resource not loaded, please open ticket </h2>" >> $MAIL_FILE
else
echo "<h2> Resource loaded successfully </h2>" >> $MAIL_FILE
fi
mail me#company.com < $MAIL_FILE
== is not a valid comparison operator in POSIX test. If your particular implementation of ksh doesn't implement an extension adding it, you may need to use
if [ "$VAR" = "NO" ]
rather than
if [ "$VAR" == "NO" ]
I'd also consider using egrep -o 'SI|NO' to leave out any other characters from the output of grep, if your copy has GNU extensions.
As another option, consider:
result=$(/home/userName/scripts/loadedresource.ksh)
case $result in
*SI*) echo "Yes" ;;
*NO*) echo "No" ;;
*) echo "Unknown" ;;
esac
As a performance enhancement, by the way:
{
echo "hello"
echo "world"
} >output.txt
...is considerably more efficient than
echo "hello" >output.txt
echo "world" >>output.txt
...which re-opens the output file once for each line.

Line break for two or more variables in batch

I have this piece of code:
echo %ip_address%>%logfile%
echo %hostname%>>%logfile%
echo %duration%>>%logfile%
However, I would like to know if it possible to echo each variable on a separate line.
I have tried the following:
echo %ip_address% && %hostname% && %duration%>c:\text.txt
echo %ip_address% & echo.%hostname% & echo.%duration%>c:\text.txt
None of these work.

why table is not created using SQLLDR in shell script?

Below is the script I use:a
#!/usr/bin/ksh
echo "create table temp_a" > emptab.sql
echo "(" >> emptab.sql
echo "subscriber_no int" >> emptab.sql
echo "); commit" >> emptab.sql
sqlplus -s user/passC#db << eof
#emptab.sql
exit
eof
After executing this script I don't get any printing or error. After executing the script I check if table exists by using for example "select * from temp_a", but I get an exception that table does not exist.
Please advise what is the error in my script.
Thanks in advance
#!/usr/bin/ksh
echo "create table temp_a" > emptab.sql
echo "(" >> emptab.sql
echo "subscriber_no int" >> emptab.sql
echo ");" >> emptab.sql #no commit
echo "/" >> emptab.sql #add /
sqlplus -s user/passC#db << eof
#emptab.sql
exit
eof

bash check value is integer and in range

I read this stackoverflow question...
Bash: check user input is correct
which does most of what I want however rather then checking it's just an integer I need to check it's an integer in a variable range....
The script looks for files in a directory and then assigns a number to them...
File 1
File 2
File 3
etc....
The user chooses the the number and the script then executes commands against that file.....the variable $FILELIST is the total number of files.
Taking the example from the previous stackoverflow I tried.....
FILENUM=""
while [[ ! ($FILENUM =~ ^[0-$FILELIST]+$) ]]; do
echo " "
echo "Please enter the file number: "
read -p "1 - $FILELIST" FILENUM < /dev/tty
done
echo "$FILENUM"
However this is throwing a syntax error: unexpected "(" (expecting "do") in the while line and I'm not sure why, I suspect $FILELIST has to be bracketed somehow but an explanation as to why the above works would help me understand the problem.
Thanks
bash-specific answers:
You don't need to reinvent the wheel: use the select builtin:
cd /path/to/directory
PS3="Select a file: "
select file in *; do
if [[ $file ]]; then break; fi
done
echo "You selected '$file'"
echo "You selected file number $REPLY"
To check a number is within a certain range, I'd write:
if (( 0 <= $number && $number <= $max )); then echo "in range"; fi
Since you're using ash you might use this as a reference: http://manpages.debian.net/cgi-bin/man.cgi?query=dash
while true; do
FILENUM=""
echo
echo "Please enter the file number: "
read -p "1 - $FILELIST" FILENUM < /dev/tty
if expr "$FILENUM" : '[0-9]\+$' &&
[ $FILENUM -gt 0 ] &&
[ $FILENUM -le $FILELIST ]
then
break
fi
done
echo "$FILENUM"