diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..4730291 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +# Keep every text file LF in the index and in working trees, whatever +# core.autocrlf says (Git for Windows defaults it to true). npm packs files as +# they are checked out, so CRLF here ends up in the published tarball (#9). +* text=auto eol=lf + +*.png binary +*.jpg binary +*.jpeg binary +*.gif binary +*.ico binary +*.exe binary diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5b83798..c5c8ce9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,4 +27,4 @@ jobs: - uses: actions/setup-node@v7 with: node-version: "24" - - run: node --test npm/install.test.js + - run: node --test npm/install.test.js npm/pack.test.js diff --git a/npm/pack.test.js b/npm/pack.test.js new file mode 100644 index 0000000..2993cbf --- /dev/null +++ b/npm/pack.test.js @@ -0,0 +1,45 @@ +// Packaging guard (run: node --test npm/pack.test.js). Packs the npm package the +// way `npm publish` would and fails if any packed file contains a CR byte, so a +// CRLF checkout (core.autocrlf=true) can't reach the registry again (#9). +// Not published: package.json "files" lists only the runtime files. +const test = require("node:test"); +const assert = require("node:assert"); +const fs = require("fs"); +const os = require("os"); +const path = require("path"); +const { execFileSync } = require("child_process"); +const { tarBin } = require("./install.js"); + +// filesWithCR returns the files under dir (relative, "/" separated) that contain a CR byte. +function filesWithCR(dir, rel = "") { + const out = []; + for (const e of fs.readdirSync(path.join(dir, rel), { withFileTypes: true })) { + const r = rel ? `${rel}/${e.name}` : e.name; + if (e.isDirectory()) out.push(...filesWithCR(dir, r)); + else if (fs.readFileSync(path.join(dir, r)).includes(0x0d)) out.push(r); + } + return out; +} + +test("filesWithCR flags CRLF files and passes LF files", () => { + const d = fs.mkdtempSync(path.join(os.tmpdir(), "togo-cr-")); + fs.mkdirSync(path.join(d, "bin")); + fs.writeFileSync(path.join(d, "ok.js"), "a\nb\n"); + fs.writeFileSync(path.join(d, "bin", "bad.js"), "a\r\nb\r\n"); + assert.deepStrictEqual(filesWithCR(d), ["bin/bad.js"]); +}); + +test("npm pack output has no CR bytes", () => { + const dest = fs.mkdtempSync(path.join(os.tmpdir(), "togo-pack-")); + const npm = process.platform === "win32" ? "npm.cmd" : "npm"; + const raw = execFileSync(npm, ["pack", "--json", "--pack-destination", dest, "--ignore-scripts"], { + cwd: __dirname, + shell: process.platform === "win32", + encoding: "utf8", + }); + const filename = JSON.parse(raw.slice(raw.indexOf("[")))[0].filename; + const unpacked = path.join(dest, "unpacked"); + fs.mkdirSync(unpacked); + execFileSync(tarBin(process.platform, process.env), ["-xzf", path.join(dest, filename), "-C", unpacked]); + assert.deepStrictEqual(filesWithCR(unpacked), [], "packed files must use LF line endings"); +});