|
1 | 1 | import { describe, it, expect, afterEach, beforeEach, vi } from "vitest"; |
2 | 2 | import { ChildProcess, execFile } from "node:child_process"; |
3 | 3 | import { mkdirSync, mkdtempSync, writeFileSync, rmSync, symlinkSync } from "node:fs"; |
4 | | -import { join } from "node:path"; |
| 4 | +import { dirname, join } from "node:path"; |
5 | 5 | import { tmpdir } from "node:os"; |
6 | 6 | import type { HyperframeLintFinding } from "./types.js"; |
7 | 7 | import { lintProject } from "./project.js"; |
@@ -399,6 +399,71 @@ describe("missing_or_empty_sub_composition", () => { |
399 | 399 | }); |
400 | 400 | }); |
401 | 401 |
|
| 402 | +describe("missing_stylesheet_asset", () => { |
| 403 | + const lintLinked = async (css: string, files: Record<string, string> = {}) => { |
| 404 | + const project = makeProject( |
| 405 | + validHtml().replace( |
| 406 | + "<body>", |
| 407 | + '<head><link rel="stylesheet" href="styles/main.css"></head><body>', |
| 408 | + ), |
| 409 | + ); |
| 410 | + mkdirSync(join(project, "styles"), { recursive: true }); |
| 411 | + writeFileSync(join(project, "styles/main.css"), css); |
| 412 | + for (const [name, body] of Object.entries(files)) { |
| 413 | + mkdirSync(dirname(join(project, name)), { recursive: true }); |
| 414 | + writeFileSync(join(project, name), body); |
| 415 | + } |
| 416 | + const { results } = await lintProject(project); |
| 417 | + return results |
| 418 | + .flatMap((entry) => entry.result.findings) |
| 419 | + .filter((finding) => finding.code === "missing_stylesheet_asset"); |
| 420 | + }; |
| 421 | + |
| 422 | + it("names the path that exists when a url() is relative to the wrong folder", async () => { |
| 423 | + const findings = await lintLinked('@font-face{font-family:X;src:url("assets/fonts/x.ttf")}', { |
| 424 | + "assets/fonts/x.ttf": "font", |
| 425 | + }); |
| 426 | + |
| 427 | + expect(findings).toHaveLength(1); |
| 428 | + expect(findings[0]).toMatchObject({ severity: "warning" }); |
| 429 | + expect(findings[0]?.message).toContain("assets/fonts/x.ttf"); |
| 430 | + expect(findings[0]?.fixHint).toContain("../assets/fonts/x.ttf"); |
| 431 | + }); |
| 432 | + |
| 433 | + it("hints the encoded url when the encoded file is in a parent folder", async () => { |
| 434 | + const findings = await lintLinked('@font-face{src:url("fonts/Open%20Sans.ttf")}', { |
| 435 | + "fonts/Open Sans.ttf": "font", |
| 436 | + }); |
| 437 | + |
| 438 | + expect(findings).toHaveLength(1); |
| 439 | + expect(findings[0]?.fixHint).toContain("../fonts/Open%20Sans.ttf"); |
| 440 | + }); |
| 441 | + |
| 442 | + it("asks for the file when it exists nowhere", async () => { |
| 443 | + const findings = await lintLinked(".a{background:url(nope.png)}"); |
| 444 | + |
| 445 | + expect(findings).toHaveLength(1); |
| 446 | + expect(findings[0]?.fixHint).toContain("Add"); |
| 447 | + }); |
| 448 | + |
| 449 | + it("stays quiet for urls that resolve beside the stylesheet, remote, data and placeholder urls", async () => { |
| 450 | + const css = [ |
| 451 | + '@font-face{src:url("../assets/fonts/x.ttf")}', |
| 452 | + ".a{background:url(img/a.png)} .b{background:url(https://cdn.example/b.png)}", |
| 453 | + ".c{background:url(data:image/png;base64,AAAA)} .d{background:url(#frag)}", |
| 454 | + ".e{background:url(${asset})}", |
| 455 | + '@font-face{src:url("fonts/Open%20Sans.ttf")}', |
| 456 | + ].join("\n"); |
| 457 | + const findings = await lintLinked(css, { |
| 458 | + "assets/fonts/x.ttf": "font", |
| 459 | + "styles/img/a.png": "png", |
| 460 | + "styles/fonts/Open Sans.ttf": "font", |
| 461 | + }); |
| 462 | + |
| 463 | + expect(findings).toEqual([]); |
| 464 | + }); |
| 465 | +}); |
| 466 | + |
402 | 467 | describe("texture_mask_asset_not_found declarations", () => { |
403 | 468 | it("still reports a missing inline mask beside a malformed declaration", async () => { |
404 | 469 | const project = makeProject( |
|
0 commit comments