Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test: scratch directories leave test/ so tree walkers stop racing them (test-reads-source-smell ENOENT); check-test-isolation R8 refuses a new one

garrytan1ff8913df8ff
diff --git a/scripts/check-test-isolation.sh b/scripts/check-test-isolation.sh
index c1c8b159a6..ff9c41d9ae 100755
--- a/scripts/check-test-isolation.sh
+++ b/scripts/check-test-isolation.sh
@@ -42,6 +42,12 @@
# beforeAll then sizes its PGLite schema from nothing, and a `-t`
# filtered run or a throw before an inline restore leaves it empty.
# Comments do not count either way (#6514).
+# R8: no scratch directory inside the source tree
+# (`join(import.meta.dir, '.tmp-...')`). Guards such as
+# test-reads-source-smell walk test/ in the same lane, and a scratch
+# directory created and removed by another file mid-walk fails them with
+# ENOENT (import-file.test.ts did this to test-reads-source-smell).
+# Use mkdtempSync(join(tmpdir(), '...')).
#
# Scope:
# - Recursively scans `test/**/*.test.ts`.
@@ -184,6 +190,8 @@ GATEWAY_CODE_SCAN='
END { if (calls != "" && !restored) printf "%s", calls }
'
UNCONFIGURE_PATTERN='(__unconfigureGatewayForTests|_clearGatewayForTests)[[:space:]]*\('
+# R8: a scratch path joined onto this file's own directory.
+SOURCE_TREE_SCRATCH_PATTERN="(import\\.meta\\.dir(name)?|__dirname)[[:space:]]*,[[:space:]]*['\"\`]\\.?tmp[-_./'\"\`]"
# R7: same comment removal; a resetGateway call counts only inside an
# afterEach/afterAll block (brace depth from the hook's line) or on the
# hook line itself. Prints the unconfigure lines when no hook restore is left.
@@ -210,7 +218,7 @@ UNCONFIGURE_CODE_SCAN='
}
END { if (calls != "" && !restored) printf "%s", calls }
'
-CANDIDATES="$(guard_candidates -E -e "$ENV_MUTATION_PATTERN" -e "$MODULE_MOCK_PATTERN" -e "$ENGINE_PATTERN" -e "$GATEWAY_PATTERN" -e "$VERDICT_PATTERN" -e "$UNCONFIGURE_PATTERN" <<< "$FILE_LIST")"
+CANDIDATES="$(guard_candidates -E -e "$ENV_MUTATION_PATTERN" -e "$MODULE_MOCK_PATTERN" -e "$ENGINE_PATTERN" -e "$GATEWAY_PATTERN" -e "$VERDICT_PATTERN" -e "$UNCONFIGURE_PATTERN" -e "$SOURCE_TREE_SCRATCH_PATTERN" <<< "$FILE_LIST")"
violations=0
file_count=0
@@ -299,6 +307,11 @@ while IFS= read -r f; do
emit_violation "$f" "R7" "__unconfigureGatewayForTests()/_clearGatewayForTests() with no resetGateway() in an afterEach/afterAll hook; an empty gateway is process-global, so the next file's beforeAll sizes its schema from nothing. Add afterEach(() => resetGateway()) (an inline restore does not survive a throw or a -t filter) or rename to *.serial.test.ts" "$unrestored"
fi
fi
+ # R8: scratch directory inside the source tree.
+ scratch_lines=$(grep -nE "$SOURCE_TREE_SCRATCH_PATTERN" "$f" 2>/dev/null || true)
+ if [ -n "$scratch_lines" ]; then
+ emit_violation "$f" "R8" "scratch directory inside the source tree; tree walkers in the same lane (test-reads-source-smell) fail with ENOENT when it is created or removed mid-walk. Use mkdtempSync(join(tmpdir(), '...'))" "$scratch_lines"
+ fi
done <<EOF
$FILE_LIST
EOF
@@ -318,6 +331,7 @@ if [ $violations -gt 0 ]; then
echo " before the run it checks) with _resetCliExitVerdictForTests()."
echo " - For __unconfigureGatewayForTests(), call resetGateway() in"
echo " afterEach or afterAll; it puts back the preload's baseline gateway."
+ echo " - For a scratch directory, use mkdtempSync(join(tmpdir(), '...'))."
echo
echo "Or, if this is a baseline file from before the lint shipped,"
echo "add it to scripts/check-test-isolation.allowlist (with a TODO"
diff --git a/test/files.test.ts b/test/files.test.ts
index b92ce57523..3d355c20a0 100644
--- a/test/files.test.ts
+++ b/test/files.test.ts
@@ -7,7 +7,7 @@ import { collectFiles, formatFileSizeKb, getMimeType, noStorageBackendMessage }
import { operationsByName } from '../src/core/operations.ts';
import * as db from '../src/core/db.ts';
-const TMP = join(import.meta.dir, '.tmp-files-test');
+const TMP = mkdtempSync(join(tmpdir(), 'gbrain-files-test-'));
// fileHash is not exported from files.ts (it takes a path there, not a
// buffer), so it stays reimplemented below. getMimeType is imported.
diff --git a/test/import-file.test.ts b/test/import-file.test.ts
index efe144c608..22f5d31534 100644
--- a/test/import-file.test.ts
+++ b/test/import-file.test.ts
@@ -1,12 +1,13 @@
import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
-import { writeFileSync, mkdirSync, rmSync, symlinkSync, readdirSync, readFileSync, existsSync } from 'fs';
+import { writeFileSync, mkdirSync, rmSync, symlinkSync, readdirSync, readFileSync, existsSync, mkdtempSync } from 'fs';
+import { tmpdir } from 'os';
import { join } from 'path';
import { importFile, importFromContent } from '../src/core/import-file.ts';
import type { BrainEngine } from '../src/core/engine.ts';
import { MARKDOWN_CHUNKER_VERSION } from '../src/core/chunkers/recursive.ts';
import { CODE_FILE_MAX_BYTES, PAGE_MAX_BYTES } from '../src/core/page-size-policy.ts';
-const TMP = join(import.meta.dir, '.tmp-import-test');
+const TMP = mkdtempSync(join(tmpdir(), 'gbrain-import-file-test-'));
// Minimal mock engine that tracks calls and supports transaction()
//
diff --git a/test/scripts/check-test-isolation-r8.test.ts b/test/scripts/check-test-isolation-r8.test.ts
new file mode 100644
index 0000000000..bbb2d3d6ec
--- /dev/null
+++ b/test/scripts/check-test-isolation-r8.test.ts
@@ -0,0 +1,58 @@
+/**
+ * Fixture tests for rule R8 of scripts/check-test-isolation.sh: a non-serial unit file may not make its scratch
+ * directory inside the source tree. test-reads-source-smell walks test/ in the same lane, and import-file.test.ts
+ * creating and removing test/.tmp-import-test mid-walk failed it with ENOENT.
+ *
+ * Same harness as check-test-isolation-r7.test.ts: the lint runs in a tmpdir on hand-written fake files.
+ */
+
+import { describe, it, expect } from 'bun:test';
+import { spawnSync } from 'child_process';
+import { mkdtempSync, mkdirSync, writeFileSync } from 'fs';
+import { tmpdir } from 'os';
+import { join, resolve } from 'path';
+
+const REPO_ROOT = resolve(import.meta.dir, '..', '..');
+const LINT_SH = resolve(REPO_ROOT, 'scripts/check-test-isolation.sh');
+
+function lintOne(contents: string, path = 'scratch.test.ts'): { status: number; stdout: string } {
+ const dir = mkdtempSync(join(tmpdir(), 'lint-isolation-r8-'));
+ mkdirSync(join(dir, 'test'), { recursive: true });
+ mkdirSync(join(dir, 'scripts'), { recursive: true });
+ writeFileSync(join(dir, 'test', path), contents);
+ writeFileSync(join(dir, 'scripts/check-test-isolation.allowlist'), '');
+ const r = spawnSync('bash', [LINT_SH, 'test'], { cwd: dir, encoding: 'utf-8' });
+ return { status: r.status ?? -1, stdout: r.stdout };
+}
+
+describe('check-test-isolation.sh R8: no scratch directory inside the source tree', () => {
+ it('bad fixtures: a .tmp or tmp path joined onto the test directory fails, naming the line', () => {
+ for (const line of [
+ // Split so this file's own source does not match the rule it tests.
+ "const TMP = join(import.meta.dir," + " '.tmp-import-test');",
+ 'const TMP = join(import.meta.dirname,' + ' ".tmp");',
+ "const TMP = path.join(__dirname," + " 'tmp/files');",
+ ]) {
+ const r = lintOne(`${line}\n`);
+ expect(r.status).toBe(1);
+ expect(r.stdout).toContain('rule R8');
+ expect(r.stdout).toContain(`1:${line}`);
+ }
+ });
+
+ it('good fixtures: a tmpdir scratch directory and fixture paths beside the test pass', () => {
+ for (const line of [
+ "const TMP = mkdtempSync(join(tmpdir(), 'gbrain-import-file-test-'));",
+ "const FIXTURE = join(import.meta.dir, 'fixtures', 'tmpl.md');",
+ "const TEMPLATES = join(import.meta.dir, 'templates');",
+ ]) {
+ const r = lintOne(`${line}\n`);
+ expect(r.stdout).toContain('check-test-isolation: OK');
+ expect(r.status).toBe(0);
+ }
+ });
+
+ it('serial files are out of scope for R8', () => {
+ expect(lintOne("const TMP = join(import.meta.dir," + " '.tmp-x');\n", 'scratch.serial.test.ts').status).toBe(0);
+ });
+});