[snap][QoL] Pragma for implicit debug mode
--- Implements popular feature request ✨ per feedback from a majority of snap users. **Add `@debug` to the first line of your `testfilter.txt` file to opt into implicit debug mode**, in which debug logging is enabled anytime filter mode is on + only one fixture file is found. - live edits to testfilter.txt are reflected in watch mode, so you can add / remove `@debug` to `testfilter.txt` in the middle of a watch session - I personally don't use debug mode all the time (I often a single file filtered + a lot of console log traces), but it should be easy to add `@debug` to the top of `testfilter.txt` and leave it there forever.
Mofei Zhang committed
Oct 4, 2023 at 14:01 UTC
3f8831fb5c7e873993ed53a77189a22bcc7395d6
3 files changed
+29
-19
compiler/packages/fixture-test-utils/src/fixture-utils.ts
+25
-18
@@ -1,16 +1,18 @@
1
import fs from "fs/promises";
2
import glob from "glob";
3
-import invariant from "invariant";
3
import path from "path";
4
import { FILTER_PATH, FIXTURES_PATH } from "./constants";
5
6
+const KIND_DEFAULT = "only";
7
export type TestFilter =
8
| {
9
kind: "only";
10
+ debug: boolean;
11
paths: Array<string>;
12
}
13
| {
14
kind: "skip";
15
+ debug: boolean;
16
paths: Array<string>;
17
};
18
@@ -30,27 +32,32 @@ export async function readTestFilter(): Promise<TestFilter | null> {
32
33
const input = await fs.readFile(FILTER_PATH, "utf8");
34
const lines = input.trim().split("\n");
33
- if (lines.length < 2) {
34
- console.warn("Misformed filter file. Expected at least two lines.");
35
- return null;
36
- }
35
38
- let filter: "only" | "skip" | null = null;
39
- if (lines[0]!.indexOf("@only") !== -1) {
40
- filter = "only";
41
- }
42
- if (lines[0]!.indexOf("@skip") !== -1) {
43
- filter = "skip";
44
- }
45
- if (filter === null) {
46
- console.warn(
47
- "Misformed filter file. Expected first line to contain @only or @skip"
48
- );
49
- return null;
36
+ let filter: "only" | "skip" = KIND_DEFAULT;
37
+ let debug: boolean = false;
38
+ const line0 = lines[0];
39
+ if (line0 != null) {
40
+ // Try to parse pragmas
41
+ let consumedLine0 = false;
42
+ if (line0.indexOf("@only") !== -1) {
43
+ filter = "only";
44
+ consumedLine0 = true;
45
+ } else if (line0.indexOf("@skip") !== -1) {
46
+ filter = "skip";
47
+ consumedLine0 = true;
48
+ }
49
+ if (line0.indexOf("@debug") !== -1) {
50
+ debug = true;
51
+ consumedLine0 = true;
52
+ }
53
+
54
+ if (consumedLine0) {
55
+ lines.shift();
56
+ }
57
}
51
- lines.shift();
58
return {
59
kind: filter,
60
+ debug,
61
paths: lines,
62
};
63
}
compiler/packages/snap/src/compiler-worker.ts
+2
-1
@@ -41,6 +41,7 @@ export async function compile(
41
loggerPath: string,
42
fixture: TestFixture,
43
compilerVersion: number,
44
+ implicitDebugMode: boolean,
45
isOnlyFixture: boolean
46
): Promise<TestResult> {
47
const seenConsoleErrors: Array<string> = [];
@@ -81,7 +82,7 @@ export async function compile(
82
// only try logging if we filtered out all but one fixture,
83
// since console log order is non-deterministic
84
const shouldLogPragma = input.split("\n")[0].includes("@debug");
84
- toggleLogging(isOnlyFixture && shouldLogPragma);
85
+ toggleLogging(isOnlyFixture && (shouldLogPragma || implicitDebugMode));
86
code = transformFixtureInput(
87
input,
88
basename,
compiler/packages/snap/src/runner.ts
+2
@@ -128,6 +128,7 @@ async function run(
128
LOGGER_PATH,
129
fixture,
130
compilerVersion,
131
+ filter?.debug ?? false,
132
isOnlyFixture
133
)
134
.then((result) => [fixtureName, result])
@@ -143,6 +144,7 @@ async function run(
144
LOGGER_PATH,
145
fixture,
146
compilerVersion,
147
+ filter?.debug ?? false,
148
isOnlyFixture
149
);
150
entries.push([fixtureName, output]);