From 1a823b21f149bbc4cb782da8589499b42e40f185 Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Wed, 17 Jun 2020 15:03:29 +0100 Subject: [PATCH] PrintAst: Emit relative paths for file nodes This is a workaround for codeql run test not itself truncating absolute paths when comparing against actual output. --- ql/src/semmle/go/PrintAst.qll | 14 ++++++++++++++ .../semmle/go/PrintAst/PrintAst.expected | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ql/src/semmle/go/PrintAst.qll b/ql/src/semmle/go/PrintAst.qll index 101303ccb7f..b425d475f15 100644 --- a/ql/src/semmle/go/PrintAst.qll +++ b/ql/src/semmle/go/PrintAst.qll @@ -173,6 +173,20 @@ class ExprNode extends BaseAstNode { } } +/** + * A node representing a `File` + */ +class FileNode extends BaseAstNode { + override File ast; + + /** + * Gets the string representation of this File. Note explicitly using a relative path + * like this rather than absolute as per default for the File class is a workaround for + * a bug with codeql run test, which should replace absolute paths but currently does not. + */ + override string toString() { result = qlClass(ast) + ast.getRelativePath() } +} + query predicate nodes(PrintAstNode node, string key, string value) { node.shouldPrint() and value = node.getProperty(key) diff --git a/ql/test/library-tests/semmle/go/PrintAst/PrintAst.expected b/ql/test/library-tests/semmle/go/PrintAst/PrintAst.expected index 7e3e021c2da..bd1454dbe7f 100644 --- a/ql/test/library-tests/semmle/go/PrintAst/PrintAst.expected +++ b/ql/test/library-tests/semmle/go/PrintAst/PrintAst.expected @@ -1,5 +1,5 @@ input.go: -# 0| [File] /home/chris/codeql-home/codeql-go/ql/test/library-tests/semmle/go/PrintAst/input.go +# 0| [File] library-tests/semmle/go/PrintAst/input.go # 3| 0: [ImportDecl] import declaration # 3| 0: [ImportSpec] import specifier # 3| 0: [StringLit] "fmt"