mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-29 19:34:29 +01:00
Design lint prints missing paths relative to your folder, also through symlinks (#4859)
`gstack-design-lint` now names a file it cannot read relative to the folder you ran it from, even when that file sits under a symlinked folder (on macOS, anything under `/tmp` or `/var`). Before, it printed the full absolute path. The design-lint tests pass on a Mac again. Rebuilt from #4856 by Akkyman2, with thanks. Nothing to do. Co-authored-by: Akkyman2 <178076175+Akkyman2@users.noreply.github.com> Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
4 files changed
+21
-6
No files matched your search
@@ -0,0 +1 @@
|
||||
`gstack-design-lint` now names a path it cannot read relative to the folder you ran it from, even when you reach that path through a symlink. Before, a missing file, a dangling link or a link that points at itself under a symlinked folder (on macOS, anything under `/tmp` or `/var`) printed its full absolute path. The design-lint tests also pass on a Mac again, where four of them compared paths against the unresolved temp folder. Thanks to Akkyman2 for the report and first fix (#4856).
|
||||
@@ -54,7 +54,7 @@ export function collectFiles(paths: string[], warn: (l: string) => void = () =>
|
||||
const visit = (p: string, top: boolean) => {
|
||||
let abs = path.resolve(p);
|
||||
let st: fs.Stats;
|
||||
try { abs = fs.realpathSync(abs); st = fs.statSync(abs); } catch { warn(`design-lint: cannot read ${p}`); if (top) skipped.push({ file: abs, kind: 'unreadable', why: 'it cannot be read' }); return; }
|
||||
try { abs = fs.realpathSync(abs); st = fs.statSync(abs); } catch { warn(`design-lint: cannot read ${p}`); if (top) skipped.push({ file: realDir(abs), kind: 'unreadable', why: 'it cannot be read' }); return; }
|
||||
if (seen.has(abs)) return;
|
||||
if (st.isDirectory()) {
|
||||
seen.add(abs);
|
||||
@@ -343,8 +343,15 @@ export function lintStaticRaw(paths: string[], opts: StaticOptions = {}): RawSta
|
||||
return { files, contexts, raw, skipped: skippedReporter.findings };
|
||||
}
|
||||
|
||||
/** A directory's real path, so a cwd or lint root reached through a symlink still matches the files collected under it. */
|
||||
export const realDir = (p: string): string => { try { return fs.realpathSync(p); } catch { return path.resolve(p); } };
|
||||
/** A path's real path, so a cwd or lint root reached through a symlink still matches the files collected under it; a path that cannot be resolved (missing, a dangling or looping link, no permission) keeps its unresolved tail under the real path of its nearest resolvable ancestor. */
|
||||
export const realDir = (p: string): string => {
|
||||
const tail: string[] = [];
|
||||
for (let dir = path.resolve(p); ; dir = path.dirname(dir)) {
|
||||
try { return path.join(fs.realpathSync(dir), ...tail.reverse()); } catch {}
|
||||
if (path.dirname(dir) === dir) return path.resolve(p);
|
||||
tail.push(path.basename(dir));
|
||||
}
|
||||
};
|
||||
|
||||
export function lintStatic(paths: string[], opts: StaticOptions = {}): StaticResult {
|
||||
const cwd = realDir(opts.cwd ?? process.cwd());
|
||||
|
||||
@@ -87,11 +87,18 @@ describe('design-lint fifth cycle: dead markup and hostile inputs', () => {
|
||||
expect(resolveRef('../outside.css', path.join(dir, 'site'), path.join(dir, 'site'))).toBeNull();
|
||||
});
|
||||
|
||||
test('an unreadable top-level path beside a readable page is one internal-error finding with its own fix', () => {
|
||||
// Value: protects=an unreadable top-level path prints relative to a cwd reached through a symlink; fails_when=the finding keeps the unresolved path while cwd is resolved; why_new=only a symlinked tmpdir (macOS) hit it; seam=none
|
||||
test('an unreadable top-level path beside a readable page is one internal-error finding with its own fix, also through a symlink', () => {
|
||||
const dir = tree({ 'index.html': page('', '<main><h1>Hi</h1><p>Body.</p></main>') });
|
||||
const r = lintStatic([dir, path.join(dir, 'nowhere.html')], { cwd: dir, ...quiet });
|
||||
expect(r.files.map((f) => path.basename(f))).toEqual(['index.html']);
|
||||
expect(only(r.findings, 'internal-error').map((f) => [f.grade, f.file, f.line, /cannot be read/.test(f.evidence), f.fix])).toEqual([['ask', 'nowhere.html', 1, true, 'check the path']]);
|
||||
const link = dir + '-link';
|
||||
fs.symlinkSync(dir, link);
|
||||
tmpDirs.push(link);
|
||||
fs.symlinkSync(path.join(dir, 'self'), path.join(dir, 'self'));
|
||||
const via = lintStatic([link, path.join(link, 'gone', 'nowhere.html'), path.join(link, 'self')], { cwd: link, ...quiet });
|
||||
expect(only(via.findings, 'internal-error').map((f) => f.file)).toEqual([path.join('gone', 'nowhere.html'), 'self']);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -43,9 +43,9 @@ function tmpPage(html: string, name = 'index.html'): string {
|
||||
return file;
|
||||
}
|
||||
|
||||
/** A temporary tree: each key is a relative path (parent directories are created), each value the file's text. */
|
||||
/** A temporary tree: each key is a relative path (parent directories are created), each value the file's text. Its root is the real path, as the lint walks real paths. */
|
||||
function tmpTree(files: Record<string, string>): string {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'design-lint-tree-'));
|
||||
const dir = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'design-lint-tree-')));
|
||||
tmpDirs.push(dir);
|
||||
for (const [rel, text] of Object.entries(files)) {
|
||||
fs.mkdirSync(path.dirname(path.join(dir, rel)), { recursive: true });
|
||||
|
||||
Reference in new issue
Block a user