Skip to content

Commit 5f88dc2

Browse files
authored
Fix minimal default scope in extract-test-databases.sh for efficient running of client integration tests (#228)
* Fix extract-test-databases.sh minimal default scope * Fixes for test db extraction on windows
1 parent 0bd5348 commit 5f88dc2

3 files changed

Lines changed: 66 additions & 7 deletions

File tree

.github/workflows/client-integration-tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ jobs:
9393
run: ./server/scripts/install-packs.sh
9494

9595
## Extract test databases used in the integration tests.
96+
## Defaults to integration scope (javascript/examples only).
97+
## Query unit tests auto-extract their own databases via `codeql test run`.
9698
- name: MCP Integration Tests - Extract test databases
9799
shell: bash
98100
run: ./server/scripts/extract-test-databases.sh

client/src/lib/integration-test-runner.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,10 +739,11 @@ export class IntegrationTestRunner {
739739
if (!fs.existsSync(absoluteDbPath) && dbPath.endsWith(".testproj")) {
740740
// For paths like "test/ExpressSqlInjection/ExpressSqlInjection.testproj",
741741
// the test source directory is "test/ExpressSqlInjection"
742-
const parts = dbPath.split(path.sep);
742+
// Always split on "/" because fixture paths use forward slashes regardless of OS.
743+
const parts = dbPath.split("/");
743744
const lastPart = parts[parts.length - 1];
744745
const testName = lastPart.replace(".testproj", "");
745-
const parentDir = parts.slice(0, -1).join(path.sep);
746+
const parentDir = parts.slice(0, -1).join("/");
746747

747748
// Check if the parent directory name matches the test name
748749
const parentDirName = parts[parts.length - 2];
@@ -785,6 +786,28 @@ export class IntegrationTestRunner {
785786
// Call the tool with appropriate parameters (timeout is handled by this.callTool)
786787
this.logger.log(`Calling tool ${toolName}`);
787788

789+
// Clean up stale interpretedOutput from prior test runs so that
790+
// directory comparisons only see output from this invocation.
791+
if (toolName === "codeql_query_run" && params.interpretedOutput) {
792+
const outputPath = String(params.interpretedOutput);
793+
const normalizedOutput = path.normalize(outputPath);
794+
// Safety: reject absolute paths and directory traversals to prevent
795+
// accidental deletion of files outside the working directory (CWE-22).
796+
if (
797+
path.isAbsolute(normalizedOutput) ||
798+
normalizedOutput.startsWith("..") ||
799+
normalizedOutput.includes(`${path.sep}..`)
800+
) {
801+
this.logger.log(` Skipping interpretedOutput cleanup: unsafe path "${outputPath}"`);
802+
} else {
803+
try {
804+
fs.rmSync(outputPath, { recursive: true, force: true });
805+
} catch {
806+
// Ignore — path may not exist yet
807+
}
808+
}
809+
}
810+
788811
const result = await this.callTool(toolName, params);
789812

790813
// For monitoring tests, we primarily check if the tool executed successfully

server/scripts/extract-test-databases.sh

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,27 @@ set -euo pipefail
33

44
## Parse command line arguments
55
LANGUAGE=""
6+
SCOPE=""
67

78
usage() {
89
cat << EOF
910
Usage: $0 [OPTIONS]
1011
1112
Extract test databases for CodeQL queries associated with the MCP server.
1213
14+
By default, only a minimal set of databases for client integration tests is
15+
pre-extracted (currently: javascript/examples only). This is not an
16+
exhaustive list of databases the integration test suite may use; additional
17+
databases may be extracted on demand, so full extraction is rarely needed.
18+
1319
OPTIONS:
14-
--language <lang> Extract databases only for the specified language
20+
--scope <scope> Extract databases for a specific use case
21+
Valid values:
22+
integration - Only databases needed by client integration tests (default)
23+
all - All test databases for all languages
24+
--language <lang> Extract databases only for the specified language (implies --scope all)
1525
Valid values: actions, cpp, csharp, go, java, javascript, python, ruby, rust, swift
1626
-h, --help Show this help message
17-
18-
By default, the script extracts databases for all supported languages.
1927
EOF
2028
}
2129

@@ -25,6 +33,10 @@ while [[ $# -gt 0 ]]; do
2533
LANGUAGE="$2"
2634
shift 2
2735
;;
36+
--scope)
37+
SCOPE="$2"
38+
shift 2
39+
;;
2840
-h|--help)
2941
usage
3042
exit 0
@@ -37,6 +49,18 @@ while [[ $# -gt 0 ]]; do
3749
esac
3850
done
3951

52+
## Validate scope if provided
53+
if [ -n "${SCOPE}" ]; then
54+
case "${SCOPE}" in
55+
integration|all) ;;
56+
*)
57+
echo "Error: Invalid scope '${SCOPE}'" >&2
58+
echo "Valid scopes: integration, all" >&2
59+
exit 1
60+
;;
61+
esac
62+
fi
63+
4064
## Validate language if provided
4165
VALID_LANGUAGES=("actions" "cpp" "csharp" "go" "java" "javascript" "python" "ruby" "rust" "swift")
4266
if [ -n "${LANGUAGE}" ]; then
@@ -91,7 +115,14 @@ extract_test_databases() {
91115
done < <(find "${_base_dir}/test" -mindepth 1 -maxdepth 1 -type d -print0)
92116
}
93117

94-
## Extract test databases for integration tests.
118+
## Extract test databases based on scope and language filters.
119+
##
120+
## Default (no flags): only databases needed by client integration tests
121+
## (currently just server/ql/javascript/examples).
122+
## --scope all: all languages × examples + tools.
123+
## --language: filter to a single language (implies --scope all).
124+
125+
# --language implies --scope all for that language
95126
if [ -n "${LANGUAGE}" ]; then
96127
echo "Extracting test databases for language: ${LANGUAGE}"
97128
# Special handling for JavaScript which has both examples and tools
@@ -101,7 +132,7 @@ if [ -n "${LANGUAGE}" ]; then
101132
if [ -d "server/ql/${LANGUAGE}/tools" ]; then
102133
extract_test_databases "server/ql/${LANGUAGE}/tools"
103134
fi
104-
else
135+
elif [ "${SCOPE}" = "all" ]; then
105136
echo "Extracting test databases for all languages..."
106137
for lang in "${VALID_LANGUAGES[@]}"; do
107138
# Special handling for JavaScript which has both examples and tools
@@ -112,6 +143,9 @@ else
112143
extract_test_databases "server/ql/${lang}/tools"
113144
fi
114145
done
146+
else
147+
echo "Extracting test databases for integration tests only..."
148+
extract_test_databases "server/ql/javascript/examples"
115149
fi
116150

117151
echo "INFO: Test database extraction complete!"

0 commit comments

Comments
 (0)