mirror of https://github.com/sveltejs/svelte
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.0 KiB
73 lines
2.0 KiB
6 years ago
|
import * as assert from "assert";
|
||
7 years ago
|
import * as fs from "fs";
|
||
|
import * as path from "path";
|
||
5 years ago
|
import * as colors from "kleur";
|
||
4 years ago
|
import { loadConfig, svelte, shouldUpdateExpected } from "../helpers";
|
||
8 years ago
|
|
||
6 years ago
|
describe("js", () => {
|
||
5 years ago
|
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
|
||
7 years ago
|
if (dir[0] === ".") return;
|
||
8 years ago
|
|
||
|
// add .solo to a sample directory name to only run that test
|
||
7 years ago
|
const solo = /\.solo/.test(dir);
|
||
8 years ago
|
|
||
7 years ago
|
if (solo && process.env.CI) {
|
||
|
throw new Error("Forgot to remove `solo: true` from test");
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
const resolved = path.resolve(`${__dirname}/samples`, dir);
|
||
5 years ago
|
|
||
5 years ago
|
if (!fs.existsSync(`${resolved}/input.svelte`)) {
|
||
5 years ago
|
console.log(colors.red().bold(`Missing file ${dir}/input.svelte. If you recently switched branches you may need to delete this directory`));
|
||
|
return;
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
(solo ? it.only : it)(dir, () => {
|
||
5 years ago
|
const config = loadConfig(`${resolved}/_config.js`);
|
||
7 years ago
|
|
||
4 years ago
|
const input = fs.readFileSync(`${resolved}/input.svelte`, "utf-8").replace(/\s+$/, "").replace(/\r/g, "");
|
||
8 years ago
|
|
||
6 years ago
|
let actual;
|
||
7 years ago
|
|
||
6 years ago
|
try {
|
||
6 years ago
|
const options = Object.assign(config.options || {});
|
||
6 years ago
|
|
||
6 years ago
|
actual = svelte.compile(input, options).js.code.replace(/generated by Svelte v\d+\.\d+\.\d+(-\w+\.\d+)?/, 'generated by Svelte vX.Y.Z');
|
||
6 years ago
|
} catch (err) {
|
||
|
console.log(err.frame);
|
||
|
throw err;
|
||
|
}
|
||
7 years ago
|
|
||
5 years ago
|
const output = `${resolved}/_actual.js`;
|
||
6 years ago
|
fs.writeFileSync(output, actual);
|
||
6 years ago
|
|
||
5 years ago
|
const expectedPath = `${resolved}/expected.js`;
|
||
6 years ago
|
|
||
5 years ago
|
let expected = '';
|
||
|
try {
|
||
|
expected = fs.readFileSync(expectedPath, "utf-8");
|
||
|
} catch (error) {
|
||
|
console.log(error);
|
||
|
if (error.code === 'ENOENT') {
|
||
|
// missing expected.js
|
||
|
fs.writeFileSync(expectedPath, actual);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
assert.equal(
|
||
4 years ago
|
actual.trim().replace(/^[ \t]+$/gm, "").replace(/\r/g, ""),
|
||
|
expected.trim().replace(/^[ \t]+$/gm, "").replace(/\r/g, "")
|
||
5 years ago
|
);
|
||
|
} catch (error) {
|
||
|
if (shouldUpdateExpected()) {
|
||
|
fs.writeFileSync(expectedPath, actual);
|
||
|
console.log(`Updated ${expectedPath}.`);
|
||
|
} else {
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
8 years ago
|
});
|
||
|
});
|
||
|
});
|