|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import * as fs from 'node:fs'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | +import { after, before, describe, it } from 'node:test'; |
| 5 | +import * as cheerio from 'cheerio'; |
| 6 | +import { type DevServer, type Fixture, loadFixture } from './test-utils.ts'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Regression test for https://github.com/withastro/astro/issues/14013 |
| 10 | + * |
| 11 | + * On case-insensitive filesystems (macOS, Windows) the dev server can be started |
| 12 | + * from a project root whose case differs from the actual on-disk case (e.g. |
| 13 | + * `d:\dev\app` vs `D:\dev\app`). `normalizeFilename` compares the configured |
| 14 | + * `root` against Vite-resolved module ids via `commonAncestorPath`, which is |
| 15 | + * case-sensitive. When the two disagree on case at the first path segment (a |
| 16 | + * Windows drive letter, or the leading directory on macOS) `commonAncestorPath` |
| 17 | + * returns `''`, so the absolute id is no longer recognized as project-internal |
| 18 | + * and gets rewritten to a bogus path. That misses the compile-metadata cache and |
| 19 | + * strips the component's scoped `<style>` from the page. |
| 20 | + * |
| 21 | + * To reproduce the discrepancy we flip the case of the first alphabetic |
| 22 | + * character of the root path. On a case-insensitive filesystem the flipped path |
| 23 | + * still resolves to the real fixture, while Vite resolves module ids with the |
| 24 | + * canonical case — exactly the mismatch from the issue. On a case-sensitive |
| 25 | + * filesystem (most Linux CI) the flipped path does not exist, so the suite is |
| 26 | + * skipped. |
| 27 | + */ |
| 28 | +const realRoot = fileURLToPath(new URL('./fixtures/css-path-case/', import.meta.url)); |
| 29 | + |
| 30 | +/** Flip the case of the first ASCII letter — the macOS leading dir or Windows drive letter. */ |
| 31 | +function flipFirstLetterCase(p: string): string { |
| 32 | + const i = p.search(/[a-zA-Z]/); |
| 33 | + if (i === -1) return p; |
| 34 | + const ch = p[i]; |
| 35 | + const flipped = ch === ch.toLowerCase() ? ch.toUpperCase() : ch.toLowerCase(); |
| 36 | + return p.slice(0, i) + flipped + p.slice(i + 1); |
| 37 | +} |
| 38 | + |
| 39 | +const caseMismatchedRoot = flipFirstLetterCase(realRoot); |
| 40 | + |
| 41 | +// Detect a case-insensitive filesystem directly rather than checking the OS: |
| 42 | +// the flipped-case path resolves to the real fixture only when the filesystem |
| 43 | +// ignores case (macOS, Windows). This is the precondition the test needs and is |
| 44 | +// more accurate than an OS check (macOS is case-insensitive too, and case |
| 45 | +// sensitivity can vary per-volume/per-directory on both macOS and Windows). |
| 46 | +const isCaseInsensitiveFs = caseMismatchedRoot !== realRoot && fs.existsSync(caseMismatchedRoot); |
| 47 | + |
| 48 | +describe('CSS scoped styles with a case-mismatched project root', { |
| 49 | + skip: !isCaseInsensitiveFs, |
| 50 | +}, () => { |
| 51 | + let fixture: Fixture; |
| 52 | + let devServer: DevServer; |
| 53 | + let $: cheerio.CheerioAPI; |
| 54 | + |
| 55 | + before(async () => { |
| 56 | + fixture = await loadFixture({ root: caseMismatchedRoot }); |
| 57 | + devServer = await fixture.startDevServer(); |
| 58 | + const html = await fixture.fetch('/').then((res) => res.text()); |
| 59 | + $ = cheerio.load(html); |
| 60 | + }); |
| 61 | + |
| 62 | + after(async () => { |
| 63 | + await devServer?.stop(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('applies the scope to the element', () => { |
| 67 | + const h1 = $('h1'); |
| 68 | + const scopedAttribute = Object.keys(h1[0]?.attribs ?? {}).find((key) => |
| 69 | + /^data-astro-cid-/.test(key), |
| 70 | + ); |
| 71 | + assert.ok(scopedAttribute, 'expected the <h1> to carry a data-astro-cid-* scope attribute'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('injects the scoped style into the page (issue #14013)', () => { |
| 75 | + const injectedStyles = $('style').text().replace(/\s/g, ''); |
| 76 | + assert.equal( |
| 77 | + injectedStyles.includes('color:rgb(255,165,0)'), |
| 78 | + true, |
| 79 | + 'expected the scoped <style> to be injected even though the root case differs from disk', |
| 80 | + ); |
| 81 | + }); |
| 82 | +}); |
0 commit comments