Skip to content

Commit 0e5cfb1

Browse files
committed
Reuse shared createMockProjectContext in preview URL tests
Extend createMockProjectContext() with options object supporting isSingleFile and config parameters. Use it in preview-initial-path tests instead of a duplicate mock function.
1 parent b6b4f23 commit 0e5cfb1

2 files changed

Lines changed: 20 additions & 29 deletions

File tree

‎tests/unit/preview-initial-path.test.ts‎

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,42 @@ import { assertEquals } from "testing/asserts";
1212
import { join } from "../../src/deno_ral/path.ts";
1313
import { normalizePath } from "../../src/core/path.ts";
1414
import { previewInitialPath } from "../../src/command/preview/preview.ts";
15-
import { ProjectContext } from "../../src/project/types.ts";
16-
17-
function mockProjectContext(
18-
dir: string,
19-
isSingleFile: boolean,
20-
): ProjectContext {
21-
return {
22-
dir: normalizePath(dir),
23-
isSingleFile,
24-
engines: [],
25-
files: { input: [], resources: [], config: [], configResources: [] },
26-
config: { project: {} },
27-
notebookContext: () => ({ resolve: () => undefined, get: () => undefined }),
28-
resolveFullMarkdownForFile: () => Promise.resolve(undefined),
29-
cleanup: () => {},
30-
} as unknown as ProjectContext;
31-
}
15+
import { createMockProjectContext } from "./project/utils.ts";
3216

3317
// deno-lint-ignore require-await
3418
unitTest("previewInitialPath - single file returns empty path (#14298)", async () => {
3519
const dir = normalizePath(Deno.makeTempDirSync({ prefix: "quarto-test" }));
3620
const outputFile = join(dir, "hello.html");
37-
const project = mockProjectContext(dir, true);
21+
const project = createMockProjectContext({ dir, isSingleFile: true });
3822

3923
const result = previewInitialPath(outputFile, project);
4024
assertEquals(result, "", "Single-file preview should use root path, not filename");
4125

42-
Deno.removeSync(dir, { recursive: true });
26+
project.cleanup();
4327
});
4428

4529
// deno-lint-ignore require-await
4630
unitTest("previewInitialPath - project file returns relative path", async () => {
4731
const dir = normalizePath(Deno.makeTempDirSync({ prefix: "quarto-test" }));
4832
const outputFile = join(dir, "chapter.html");
49-
const project = mockProjectContext(dir, false);
33+
const project = createMockProjectContext({ dir });
5034

5135
const result = previewInitialPath(outputFile, project);
5236
assertEquals(result, "chapter.html", "Project preview should include relative path");
5337

54-
Deno.removeSync(dir, { recursive: true });
38+
project.cleanup();
5539
});
5640

5741
// deno-lint-ignore require-await
5842
unitTest("previewInitialPath - project subdir returns relative path", async () => {
5943
const dir = normalizePath(Deno.makeTempDirSync({ prefix: "quarto-test" }));
6044
const outputFile = join(dir, "pages", "about.html");
61-
const project = mockProjectContext(dir, false);
45+
const project = createMockProjectContext({ dir });
6246

6347
const result = previewInitialPath(outputFile, project);
6448
assertEquals(result, "pages/about.html", "Project preview should include subdirectory path");
6549

66-
Deno.removeSync(dir, { recursive: true });
50+
project.cleanup();
6751
});
6852

6953
// deno-lint-ignore require-await

‎tests/unit/project/utils.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,28 @@ import { FileInformationCacheMap } from "../../../src/project/project-shared.ts"
1111

1212
/**
1313
* Create a minimal mock ProjectContext for testing.
14-
* Only provides the essential properties needed for cache-related tests.
1514
*
16-
* @param dir - The project directory (defaults to a temp directory)
15+
* @param options.dir - The project directory (defaults to a temp directory)
16+
* @param options.isSingleFile - Whether this is a single-file project (defaults to false)
17+
* @param options.config - Project config (defaults to { project: {} })
1718
* @returns A mock ProjectContext suitable for unit testing
1819
*/
1920
export function createMockProjectContext(
20-
dir?: string,
21+
options?: {
22+
dir?: string;
23+
isSingleFile?: boolean;
24+
config?: ProjectContext["config"];
25+
},
2126
): ProjectContext {
22-
const projectDir = dir ?? Deno.makeTempDirSync({ prefix: "quarto-test" });
23-
const ownsDir = dir === undefined;
27+
const projectDir = options?.dir ??
28+
Deno.makeTempDirSync({ prefix: "quarto-test" });
29+
const ownsDir = options?.dir === undefined;
2430

2531
return {
2632
dir: projectDir,
2733
engines: [],
2834
files: { input: [] },
35+
config: options?.config ?? { project: {} },
2936
notebookContext: {} as ProjectContext["notebookContext"],
3037
fileInformationCache: new FileInformationCacheMap(),
3138
resolveBrand: () => Promise.resolve(undefined),
@@ -37,7 +44,7 @@ export function createMockProjectContext(
3744
clone: function () {
3845
return this;
3946
},
40-
isSingleFile: false,
47+
isSingleFile: options?.isSingleFile ?? false,
4148
diskCache: {} as ProjectContext["diskCache"],
4249
temp: {} as ProjectContext["temp"],
4350
cleanup: () => {

0 commit comments

Comments
 (0)