Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tests/unit/file-permissions.test.ts
12924 views
1
/*
2
* file-permissions.test.ts
3
*
4
* Copyright (C) 2020-2026 Posit Software, PBC
5
*/
6
7
import { unitTest } from "../test.ts";
8
import { withTempDir } from "../utils.ts";
9
import { assert, assertEquals } from "testing/asserts";
10
import { join } from "../../src/deno_ral/path.ts";
11
import { isWindows } from "../../src/deno_ral/platform.ts";
12
import {
13
ensureUserWritable,
14
safeModeFromFile,
15
} from "../../src/deno_ral/fs.ts";
16
17
function writeFile(dir: string, name: string, content: string, mode: number): string {
18
const path = join(dir, name);
19
Deno.writeTextFileSync(path, content);
20
Deno.chmodSync(path, mode);
21
return path;
22
}
23
24
unitTest(
25
"file-permissions - ensureUserWritable fixes read-only files",
26
async () => withTempDir((dir) => {
27
const file = writeFile(dir, "readonly.txt", "test content", 0o444);
28
29
const modeBefore = safeModeFromFile(file);
30
assert(modeBefore !== undefined);
31
assert((modeBefore! & 0o200) === 0, "File should be read-only before fix");
32
33
ensureUserWritable(file);
34
35
assertEquals(safeModeFromFile(file)! & 0o777, 0o644,
36
"Permission bits should be 0o644 (0o444 | 0o200) — only user write bit added");
37
}),
38
{ ignore: isWindows },
39
);
40
41
unitTest(
42
"file-permissions - ensureUserWritable leaves writable files unchanged",
43
async () => withTempDir((dir) => {
44
const file = writeFile(dir, "writable.txt", "test content", 0o644);
45
const modeBefore = safeModeFromFile(file);
46
assert(modeBefore !== undefined, "Mode should be readable");
47
48
ensureUserWritable(file);
49
50
assertEquals(safeModeFromFile(file), modeBefore,
51
"Mode should be unchanged for already-writable file");
52
}),
53
{ ignore: isWindows },
54
);
55
56
// Simulates the Nix/deb scenario: Deno.copyFileSync from a read-only source
57
// preserves the read-only mode on the copy. ensureUserWritable must fix it.
58
unitTest(
59
"file-permissions - copyFileSync from read-only source then ensureUserWritable",
60
async () => withTempDir((dir) => {
61
const src = writeFile(dir, "source.lua", "-- filter code", 0o444);
62
63
// Copy it (this is what quarto create does internally)
64
const dest = join(dir, "dest.lua");
65
Deno.copyFileSync(src, dest);
66
67
// Without the fix, dest inherits 0o444 from src
68
const modeBefore = safeModeFromFile(dest);
69
assert(modeBefore !== undefined);
70
assert((modeBefore! & 0o200) === 0, "Copied file should inherit read-only mode from source");
71
72
// Make source writable so cleanup succeeds
73
Deno.chmodSync(src, 0o644);
74
75
ensureUserWritable(dest);
76
77
assertEquals(safeModeFromFile(dest)! & 0o777, 0o644,
78
"Copied file should be user-writable after ensureUserWritable");
79
}),
80
{ ignore: isWindows },
81
);
82
83