From 23b8af3a56bbf6543862e042e8757c49b465e403 Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Mon, 29 Mar 2021 11:34:50 +0100 Subject: [PATCH 1/2] Tolerate empty-string CODEQL_PLATFORM This is normal when invoked with tracing disabled, so we also don't log when this happens. --- extractor/util/util.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/extractor/util/util.go b/extractor/util/util.go index 57f4597ec22..4bd7573d7e1 100644 --- a/extractor/util/util.go +++ b/extractor/util/util.go @@ -145,9 +145,7 @@ func RunCmd(cmd *exec.Cmd) bool { func getOsToolsSubdir() (string, error) { platform, set := os.LookupEnv("CODEQL_PLATFORM") - if !set { - log.Print("CODEQL_PLATFORM not set; this binary should be run from the `codeql` CLI. Falling back to use `runtime.GOOS`.\n") - } else { + if set && platform != "" { return platform, nil } From 87d8bc8d6f692348d0c325476562d611aa631343 Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Mon, 29 Mar 2021 13:08:47 +0100 Subject: [PATCH 2/2] Add basic extractor smoke test This exercises the extractor via 'codeql', with and without tracing. --- Makefile | 1 + extractor-smoke-test/.gitignore | 4 ++++ extractor-smoke-test/expected.csv | 12 ++++++++++++ extractor-smoke-test/go.mod | 3 +++ extractor-smoke-test/main.go | 8 ++++++++ extractor-smoke-test/test.sh | 24 ++++++++++++++++++++++++ 6 files changed, 52 insertions(+) create mode 100644 extractor-smoke-test/.gitignore create mode 100644 extractor-smoke-test/expected.csv create mode 100644 extractor-smoke-test/go.mod create mode 100644 extractor-smoke-test/main.go create mode 100755 extractor-smoke-test/test.sh diff --git a/Makefile b/Makefile index d4631e810cc..f9a145f16ea 100644 --- a/Makefile +++ b/Makefile @@ -118,6 +118,7 @@ test: all build/testdb/check-upgrade-path # use GOOS=linux because GOOS=darwin GOARCH=386 is no longer supported env GOOS=linux GOARCH=386 codeql$(EXE) test run ql/test/query-tests/Security/CWE-681 --search-path . --consistency-queries ql/test/consistency cd extractor; go test -mod=vendor ./... | grep -vF "[no test files]" + bash extractor-smoke-test/test.sh || (echo "Extractor smoke test FAILED"; exit 1) .PHONY: build/testdb/check-upgrade-path build/testdb/check-upgrade-path : build/testdb/go.dbscheme ql/src/go.dbscheme diff --git a/extractor-smoke-test/.gitignore b/extractor-smoke-test/.gitignore new file mode 100644 index 00000000000..e32b11d7ee1 --- /dev/null +++ b/extractor-smoke-test/.gitignore @@ -0,0 +1,4 @@ +*.bqrs +tracing-out.csv +notracing-out.csv +testdb diff --git a/extractor-smoke-test/expected.csv b/extractor-smoke-test/expected.csv new file mode 100644 index 00000000000..6a553d64913 --- /dev/null +++ b/extractor-smoke-test/expected.csv @@ -0,0 +1,12 @@ +"nd","col1" +"entry","skip" +"entry","skip" +"function declaration","exit" +"assignment to i","selection of Println" +"skip","skip" +"skip","function declaration" +"skip","1" +"1","assignment to i" +"call to Println","exit" +"selection of Println","i" +"i","call to Println" diff --git a/extractor-smoke-test/go.mod b/extractor-smoke-test/go.mod new file mode 100644 index 00000000000..0659b4eaec8 --- /dev/null +++ b/extractor-smoke-test/go.mod @@ -0,0 +1,3 @@ +module github.com/codeql-go-extractor-smoke-test + +go 1.14 diff --git a/extractor-smoke-test/main.go b/extractor-smoke-test/main.go new file mode 100644 index 00000000000..2f7b1325daa --- /dev/null +++ b/extractor-smoke-test/main.go @@ -0,0 +1,8 @@ +package main + +import "fmt" + +func main() { + var i int = 1 + fmt.Println(i) +} diff --git a/extractor-smoke-test/test.sh b/extractor-smoke-test/test.sh new file mode 100755 index 00000000000..387416d8e4d --- /dev/null +++ b/extractor-smoke-test/test.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +set -e + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +cd $DIR + +rm -rf testdb + +codeql database create --language=go testdb --search-path .. +codeql query run ../ql/test/library-tests/semmle/go/controlflow/ControlFlowGraph/ControlFlowNode_getASuccessor.ql --database=testdb --output=notracing-out.bqrs --search-path .. +codeql bqrs decode notracing-out.bqrs --format=csv --output=notracing-out.csv +diff -w -u notracing-out.csv expected.csv + +# Now do it again with tracing enabled + +export CODEQL_EXTRACTOR_GO_BUILD_TRACING=on + +rm -rf testdb + +codeql database create --language=go testdb --search-path .. +codeql query run ../ql/test/library-tests/semmle/go/controlflow/ControlFlowGraph/ControlFlowNode_getASuccessor.ql --database=testdb --output=tracing-out.bqrs --search-path .. +codeql bqrs decode tracing-out.bqrs --format=csv --output=tracing-out.csv +diff -w -u tracing-out.csv expected.csv