How do I exclude test files from makefile shell command? - go

I'm tyring to exclude test files in my makefile shell command.
When i run the following makefile target,
api-run:
${API_COMPOSE} sh -c "go run -mod=vendor cmd/serverd/*.go"
I get this error
go: cannot run *_test.go files (cmd/serverd/router_test.go)
make: *** [api-run] Error 1
I've tried adjusting the command into
api-run:
${API_COMPOSE} sh -c "for file in \$(ls cmd/serverd/*.go | grep -v _test.go); do go run -mod=vendor \$file; done"
but end up getting this error
sh: 1: Syntax error: "done" unexpected (expecting "do")
make: *** [api-run] Error 2

Related

buildkite syntax error when running code coverage on golang

I'm running the following make target in a buildkite step
cover:
go test ./... -coverprofile=coverage.out
ccover=$$(go tool cover -func cover.out | grep total | grep -Eo '[0-9]+\.[0-9]+');
if [ $$(bc <<< "$$ccover < 20.0") -eq 1 ]; then\
exit 1;\
fi
This is what my buildkite yaml looks like
- label: ":coverage: check code coverage"
key: "cvg"
command:
- make cover
Now when this executes on the server I get the following error
/bin/sh: syntax error: unexpected redirection
make: *** [Makefile:20: analysecover] Error 2
Error: The command exited with status 1
Any idea on what could be going wrong here ?

How can I resolve 'Make clean' error in Command Prompt

I created a batch file, for a project I am working on, which works as expected from a command in MINGW64, but not from a command in CMD.
I have the same problem with 'make clean', which, only in CMD prints an error.
make[1]: Entering directory 'C:/Users/src/Probs'
rm -rf ./obj ./mod
process_begin: CreateProcess(NULL, rm -rf ./obj ./mod, ...) failed.
make (e=2): The system cannot find the file specified.
make[1]: *** [makefile:114: clean] Error 2
make[1]: Leaving directory 'C:/Users/src/Probs'
make: *** [makefile:56: clean] Error 2
Would you please comment on that?
When you run your script in command prompt it doesn't recognize the rm command. It doesn't exist on command prompt.
You can change it to use rd instead:
rd /s /q "path"
Or you can install cygwin, which has rm as well as ls etc...
And add "Cygwin\bin" to your Environment Path variable

How to escape parenthesis for the cp command inside a Makefile

I have this Makefile:
VAR=foo(1).txt foo(2).txt
foobar: $VAR
cp -p $^ foo/
When I run it I get this error:
$ make test
/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `cp -p foo(1).txt foo(2).txt foo/'
Makefile:3: recipe for target 'foobar' failed
make: *** [test] Error 1
How to quickly get rid of it?
You can wrapt the file names in double quotes
cp "foo(1).txt" "foo(2).txt" /out
Test
$ cp "foo(1).txt" "foo(2).txt" out/
$ ls out/
foo(1).txt foo(2).txt
Or much safer would be
cp 'foo(1).txt' 'foo(2).txt' out/

How to fix Makefile syntax error when using wildcard on make clean?

I have a simple Makefile that just contains this one target. It looks like this:
SHELL:=/bin/bash
clean:
rm !(*.tex|Makefile|*.pdf)
When I run this command in bash it works fine, i.e. it gives no errors and it removes the desired files. However when I run make clean it gives the following errors:
$ make clean
rm !(*.tex|Makefile|*.pdf)
/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `rm !(*.tex|Makefile|*.pdf)'
make: *** [clean] Error 1
Has anybody got an idea what I'm doing wrong? Thanks.
Change the SHELL line to
SHELL:=/bin/bash -O extglob
The extglob option is not set by default, so you have to do that yourself.

Makefile delete all files but one [duplicate]

I have a simple Makefile that just contains this one target. It looks like this:
SHELL:=/bin/bash
clean:
rm !(*.tex|Makefile|*.pdf)
When I run this command in bash it works fine, i.e. it gives no errors and it removes the desired files. However when I run make clean it gives the following errors:
$ make clean
rm !(*.tex|Makefile|*.pdf)
/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `rm !(*.tex|Makefile|*.pdf)'
make: *** [clean] Error 1
Has anybody got an idea what I'm doing wrong? Thanks.
Change the SHELL line to
SHELL:=/bin/bash -O extglob
The extglob option is not set by default, so you have to do that yourself.

Resources