Skip to content

Commit bfead07

Browse files
committed
Move walkDirectory to pure files file
1 parent 8c98401 commit bfead07

File tree

6 files changed

+97
-106
lines changed

6 files changed

+97
-106
lines changed

extensions/ql-vscode/src/codeql-cli/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ import {
2222
getErrorMessage,
2323
getErrorStack,
2424
} from "../pure/helpers-pure";
25+
import { walkDirectory } from "../pure/files";
2526
import { QueryMetadata, SortDirection } from "../pure/interface-types";
2627
import { BaseLogger, Logger, ProgressReporter } from "../common";
2728
import { CompilationMessage } from "../pure/legacy-messages";
2829
import { sarifParser } from "../common/sarif-parser";
29-
import { walkDirectory } from "../helpers";
3030
import { App } from "../common/app";
3131
import { QueryLanguage } from "../common/query-language";
3232

extensions/ql-vscode/src/helpers.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
ensureDirSync,
3-
pathExists,
4-
ensureDir,
5-
writeFile,
6-
opendir,
7-
} from "fs-extra";
1+
import { ensureDirSync, pathExists, ensureDir, writeFile } from "fs-extra";
82
import { join, dirname } from "path";
93
import { dirSync } from "tmp-promise";
104
import { Uri, window as Window, workspace, env, WorkspaceFolder } from "vscode";
@@ -455,29 +449,6 @@ export async function createTimestampFile(storagePath: string) {
455449
await writeFile(timestampPath, Date.now().toString(), "utf8");
456450
}
457451

458-
/**
459-
* Recursively walk a directory and return the full path to all files found.
460-
* Symbolic links are ignored.
461-
*
462-
* @param dir the directory to walk
463-
*
464-
* @return An iterator of the full path to all files recursively found in the directory.
465-
*/
466-
export async function* walkDirectory(
467-
dir: string,
468-
): AsyncIterableIterator<string> {
469-
const seenFiles = new Set<string>();
470-
for await (const d of await opendir(dir)) {
471-
const entry = join(dir, d.name);
472-
seenFiles.add(entry);
473-
if (d.isDirectory()) {
474-
yield* walkDirectory(entry);
475-
} else if (d.isFile()) {
476-
yield entry;
477-
}
478-
}
479-
}
480-
481452
/**
482453
* Returns the path of the first folder in the workspace.
483454
* This is used to decide where to create skeleton QL packs.

extensions/ql-vscode/src/pure/files.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pathExists, stat, readdir } from "fs-extra";
1+
import { pathExists, stat, readdir, opendir } from "fs-extra";
22
import { join, resolve } from "path";
33

44
/**
@@ -88,3 +88,26 @@ export async function readDirFullPaths(path: string): Promise<string[]> {
8888
const baseNames = await readdir(path);
8989
return baseNames.map((baseName) => join(path, baseName));
9090
}
91+
92+
/**
93+
* Recursively walk a directory and return the full path to all files found.
94+
* Symbolic links are ignored.
95+
*
96+
* @param dir the directory to walk
97+
*
98+
* @return An iterator of the full path to all files recursively found in the directory.
99+
*/
100+
export async function* walkDirectory(
101+
dir: string,
102+
): AsyncIterableIterator<string> {
103+
const seenFiles = new Set<string>();
104+
for await (const d of await opendir(dir)) {
105+
const entry = join(dir, d.name);
106+
seenFiles.add(entry);
107+
if (d.isDirectory()) {
108+
yield* walkDirectory(entry);
109+
} else if (d.isFile()) {
110+
yield entry;
111+
}
112+
}
113+
}

extensions/ql-vscode/test/unit-tests/pure/files.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import {
66
getDirectoryNamesInsidePath,
77
pathsEqual,
88
readDirFullPaths,
9+
walkDirectory,
910
} from "../../../src/pure/files";
11+
import { DirResult } from "tmp";
12+
import * as tmp from "tmp";
13+
import { ensureDirSync, symlinkSync, writeFileSync } from "fs-extra";
1014

1115
describe("files", () => {
1216
const dataDir = join(__dirname, "../../data");
@@ -299,3 +303,67 @@ describe("containsPath", () => {
299303
},
300304
);
301305
});
306+
307+
describe("walkDirectory", () => {
308+
let tmpDir: DirResult;
309+
let dir: string;
310+
let dir2: string;
311+
312+
beforeEach(() => {
313+
tmpDir = tmp.dirSync({ unsafeCleanup: true });
314+
dir = join(tmpDir.name, "dir");
315+
ensureDirSync(dir);
316+
dir2 = join(tmpDir.name, "dir2");
317+
});
318+
319+
afterEach(() => {
320+
tmpDir.removeCallback();
321+
});
322+
323+
it("should walk a directory", async () => {
324+
const file1 = join(dir, "file1");
325+
const file2 = join(dir, "file2");
326+
const file3 = join(dir, "file3");
327+
const dir3 = join(dir, "dir3");
328+
const file4 = join(dir, "file4");
329+
const file5 = join(dir, "file5");
330+
const file6 = join(dir, "file6");
331+
332+
// These symlinks link back to paths that are already existing, so ignore.
333+
const symLinkFile7 = join(dir, "symlink0");
334+
const symlinkDir = join(dir2, "symlink1");
335+
336+
// some symlinks that point outside of the base dir.
337+
const file8 = join(tmpDir.name, "file8");
338+
const file9 = join(dir2, "file8");
339+
const symlinkDir2 = join(dir2, "symlink2");
340+
const symlinkFile2 = join(dir2, "symlinkFile3");
341+
342+
ensureDirSync(dir2);
343+
ensureDirSync(dir3);
344+
345+
writeFileSync(file1, "file1");
346+
writeFileSync(file2, "file2");
347+
writeFileSync(file3, "file3");
348+
writeFileSync(file4, "file4");
349+
writeFileSync(file5, "file5");
350+
writeFileSync(file6, "file6");
351+
writeFileSync(file8, "file8");
352+
writeFileSync(file9, "file9");
353+
354+
// We don't really need to be testing all of these variants of symlinks,
355+
// but it doesn't hurt, and will help us if we ever do decide to support them.
356+
symlinkSync(file6, symLinkFile7, "file");
357+
symlinkSync(dir3, symlinkDir, "dir");
358+
symlinkSync(file8, symlinkFile2, "file");
359+
symlinkSync(dir2, symlinkDir2, "dir");
360+
361+
const files = [];
362+
for await (const file of walkDirectory(dir)) {
363+
files.push(file);
364+
}
365+
366+
// Only real files should be returned.
367+
expect(files.sort()).toEqual([file1, file2, file3, file4, file5, file6]);
368+
});
369+
});

extensions/ql-vscode/test/vscode-tests/no-workspace/helpers.test.ts

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { Uri, window, workspace, WorkspaceFolder } from "vscode";
22
import * as tmp from "tmp";
33
import { join } from "path";
4-
import {
5-
writeFileSync,
6-
ensureDirSync,
7-
symlinkSync,
8-
writeFile,
9-
mkdir,
10-
} from "fs-extra";
11-
import { DirResult } from "tmp";
4+
import { writeFile, mkdir } from "fs-extra";
125

136
import {
147
getFirstWorkspaceFolder,
@@ -18,7 +11,6 @@ import {
1811
showBinaryChoiceWithUrlDialog,
1912
showInformationMessageWithAction,
2013
showNeverAskAgainDialog,
21-
walkDirectory,
2214
} from "../../../src/helpers";
2315
import { reportStreamProgress } from "../../../src/common/vscode/progress";
2416
import { Setting } from "../../../src/config";
@@ -243,70 +235,6 @@ describe("helpers", () => {
243235
});
244236
});
245237

246-
describe("walkDirectory", () => {
247-
let tmpDir: DirResult;
248-
let dir: string;
249-
let dir2: string;
250-
251-
beforeEach(() => {
252-
tmpDir = tmp.dirSync({ unsafeCleanup: true });
253-
dir = join(tmpDir.name, "dir");
254-
ensureDirSync(dir);
255-
dir2 = join(tmpDir.name, "dir2");
256-
});
257-
258-
afterEach(() => {
259-
tmpDir.removeCallback();
260-
});
261-
262-
it("should walk a directory", async () => {
263-
const file1 = join(dir, "file1");
264-
const file2 = join(dir, "file2");
265-
const file3 = join(dir, "file3");
266-
const dir3 = join(dir, "dir3");
267-
const file4 = join(dir, "file4");
268-
const file5 = join(dir, "file5");
269-
const file6 = join(dir, "file6");
270-
271-
// These symlinks link back to paths that are already existing, so ignore.
272-
const symLinkFile7 = join(dir, "symlink0");
273-
const symlinkDir = join(dir2, "symlink1");
274-
275-
// some symlinks that point outside of the base dir.
276-
const file8 = join(tmpDir.name, "file8");
277-
const file9 = join(dir2, "file8");
278-
const symlinkDir2 = join(dir2, "symlink2");
279-
const symlinkFile2 = join(dir2, "symlinkFile3");
280-
281-
ensureDirSync(dir2);
282-
ensureDirSync(dir3);
283-
284-
writeFileSync(file1, "file1");
285-
writeFileSync(file2, "file2");
286-
writeFileSync(file3, "file3");
287-
writeFileSync(file4, "file4");
288-
writeFileSync(file5, "file5");
289-
writeFileSync(file6, "file6");
290-
writeFileSync(file8, "file8");
291-
writeFileSync(file9, "file9");
292-
293-
// We don't really need to be testing all of these variants of symlinks,
294-
// but it doesn't hurt, and will help us if we ever do decide to support them.
295-
symlinkSync(file6, symLinkFile7, "file");
296-
symlinkSync(dir3, symlinkDir, "dir");
297-
symlinkSync(file8, symlinkFile2, "file");
298-
symlinkSync(dir2, symlinkDir2, "dir");
299-
300-
const files = [];
301-
for await (const file of walkDirectory(dir)) {
302-
files.push(file);
303-
}
304-
305-
// Only real files should be returned.
306-
expect(files.sort()).toEqual([file1, file2, file3, file4, file5, file6]);
307-
});
308-
});
309-
310238
describe("isFolderAlreadyInWorkspace", () => {
311239
beforeEach(() => {
312240
const folders = [

extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/variant-analysis-history.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { join } from "path";
1010

1111
import { ExtensionContext, Uri } from "vscode";
1212
import { DatabaseManager } from "../../../../src/databases/local-databases";
13-
import { tmpDir, walkDirectory } from "../../../../src/helpers";
13+
import { tmpDir } from "../../../../src/helpers";
14+
import { walkDirectory } from "../../../../src/pure/files";
1415
import { DisposableBucket } from "../../disposable-bucket";
1516
import { testDisposeHandler } from "../../test-dispose-handler";
1617
import { HistoryItemLabelProvider } from "../../../../src/query-history/history-item-label-provider";

0 commit comments

Comments
 (0)