Skip to content

Commit 1846e4d

Browse files
Update logging
1 parent ea5a9c5 commit 1846e4d

8 files changed

Lines changed: 126 additions & 31 deletions

File tree

.pre-commit-hooks.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
- id: snippet-fmt
22
name: Format snippet files
3-
entry: snippet-fmt --quiet
3+
entry: snippet-fmt
44
language: node
55
files: \.snippet$
66

77
- id: talon-fmt
88
name: Format Talon files
9-
entry: talon-fmt --quiet
9+
entry: talon-fmt
1010
language: node
1111
files: \.(talon|talon-list)$
1212

1313
- id: tree-sitter-fmt
1414
name: Format Tree-sitter query files
15-
entry: tree-sitter-fmt --quiet
15+
entry: tree-sitter-fmt
1616
language: node
1717
files: \.scm$

out/snippetFormatter.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

out/talonFormatter.js

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

out/treeSitterFormatter.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cursorless/talon-tools",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"description": "Linting and formatting tools for Talon and Cursorless",
55
"author": "Cursorless Dev",
66
"license": "MIT",

src/cli/cli.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,6 @@ async function mainFormatFiles({
119119
return EXIT_OK;
120120
}
121121

122-
if (changedFileCount > 0) {
123-
logger.log(`Formatted ${changedFileCount} file(s).`);
124-
} else {
125-
logger.log("All files are already formatted.");
126-
}
127-
128122
return EXIT_OK;
129123
}
130124

@@ -186,10 +180,9 @@ export async function formatFile({
186180
return false;
187181
}
188182

189-
if (check) {
190-
logger.warn(filePath);
191-
} else {
192-
logger.log(filePath);
183+
logger.log(filePath);
184+
185+
if (!check) {
193186
await fs.writeFile(filePath, formatted, "utf8");
194187
}
195188

src/test/cli.test.ts

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import fs from "node:fs/promises";
55
import os from "node:os";
66
import path from "node:path";
77
import { PassThrough } from "node:stream";
8-
import { formatFile, formatFiles, mainFormatStdin } from "../cli/cli.js";
8+
import { formatFile, formatFiles, main, mainFormatStdin } from "../cli/cli.js";
99
import type {
1010
CLI,
1111
EditorConfigOptions,
@@ -67,6 +67,9 @@ suite("CLI", () => {
6767

6868
assert.equal(didChange, true);
6969
assert.equal(actual, "content");
70+
assert.deepEqual(logger.getEntries(), [
71+
{ level: "log", message: fileName },
72+
]);
7073
} finally {
7174
await cleanupTempFile(fileName);
7275
}
@@ -301,6 +304,74 @@ suite("CLI", () => {
301304
assert.equal(output.text, "[warn] Code style issues found in stdin.\n");
302305
});
303306

307+
test("Captures check-mode file entries without writing when quiet", async () => {
308+
const fileName = await createTempFile(
309+
"talonfmt-",
310+
"example.txt",
311+
"content",
312+
);
313+
const cli = createCLI((text) => `${text} updated`);
314+
const logger = createLogger(true);
315+
316+
try {
317+
const stdout = await captureStreamWrite(process.stdout, async () =>
318+
formatFile({
319+
cli,
320+
logger,
321+
check: true,
322+
debug: false,
323+
filePath: fileName,
324+
}),
325+
);
326+
const stderr = await captureStreamWrite(process.stderr, async () =>
327+
Promise.resolve(),
328+
);
329+
330+
assert.equal(stdout.result, true);
331+
assert.equal(stdout.text, "");
332+
assert.equal(stderr.text, "");
333+
assert.deepEqual(logger.getEntries(), [
334+
{ level: "log", message: fileName },
335+
]);
336+
} finally {
337+
await cleanupTempFile(fileName);
338+
}
339+
});
340+
341+
test("Check mode reports summary and changed file paths", async () => {
342+
const fileName = await createTempFile(
343+
"talonfmt-",
344+
"example.txt",
345+
"content",
346+
);
347+
const cli = createCLI((text) => `${text} updated`);
348+
const originalArgv = process.argv;
349+
const originalExitCode = process.exitCode;
350+
351+
try {
352+
process.argv = ["node", "talon-fmt", "--check", fileName];
353+
354+
const output = await captureStdoutAndStderr(async () => {
355+
await main(cli);
356+
return process.exitCode;
357+
});
358+
359+
assert.equal(output.result, EXIT_FAIL);
360+
assert.equal(
361+
output.stdoutText,
362+
["Checking formatting...", `${fileName}`, ""].join("\n"),
363+
);
364+
assert.equal(
365+
output.stderrText,
366+
"[warn] Code style issues found in 1 file(s).\n",
367+
);
368+
} finally {
369+
process.argv = originalArgv;
370+
process.exitCode = originalExitCode;
371+
await cleanupTempFile(fileName);
372+
}
373+
});
374+
304375
test("Returns success for unchanged stdin in check mode", async () => {
305376
const cli = createCLI((text) => text);
306377
const logger = createLogger();
@@ -606,6 +677,37 @@ async function captureStreamWrite<T>(
606677
}
607678
}
608679

680+
async function captureStdoutAndStderr<T>(
681+
callback: () => Promise<T>,
682+
): Promise<{ result: T; stdoutText: string; stderrText: string }> {
683+
let stdoutText = "";
684+
let stderrText = "";
685+
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
686+
const originalStderrWrite = process.stderr.write.bind(process.stderr);
687+
688+
(process.stdout.write as unknown as (chunk: string) => boolean) = (
689+
chunk: string | Uint8Array,
690+
) => {
691+
stdoutText += chunk.toString();
692+
return true;
693+
};
694+
695+
(process.stderr.write as unknown as (chunk: string) => boolean) = (
696+
chunk: string | Uint8Array,
697+
) => {
698+
stderrText += chunk.toString();
699+
return true;
700+
};
701+
702+
try {
703+
const result = await callback();
704+
return { result, stdoutText, stderrText };
705+
} finally {
706+
process.stdout.write = originalStdoutWrite;
707+
process.stderr.write = originalStderrWrite;
708+
}
709+
}
710+
609711
function getArguments(args: Partial<ParsedArgs>) {
610712
return {
611713
...getDefaultArguments(),

0 commit comments

Comments
 (0)