refactor test/sourcemaps

use _config.js, indent with tabs, rename variables,
pull/5428/head
Milan Hauth 5 years ago
parent e753489455
commit 6d06b7b8da

@ -1,7 +1,9 @@
import * as fs from "fs";
import * as path from "path";
import * as assert from "assert";
import { svelte } from "../helpers.js";
import { loadConfig, svelte } from "../helpers.js";
// keep source-map at version 0.7.x
// https://github.com/mozilla/source-map/issues/400
import { SourceMapConsumer } from "source-map";
import { getLocator } from "locate-character";
@ -9,81 +11,80 @@ describe("sourcemaps", () => {
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
if (dir[0] === ".") return;
const config = loadConfig(`${__dirname}/samples/${dir}/_config.js`);
// add .solo to a sample directory name to only run that test
const solo = /\.solo/.test(dir);
const skip = /\.skip/.test(dir);
const solo = config.solo || /\.solo/.test(dir);
const skip = config.skip || /\.skip/.test(dir);
if (solo && process.env.CI) {
throw new Error("Forgot to remove `solo: true` from test");
}
(solo ? it.only : skip ? it.skip : it)(dir, async () => {
const filename = path.resolve(
`${__dirname}/samples/${dir}/input.svelte`
);
const outputFilename = path.resolve(
`${__dirname}/samples/${dir}/output`
);
const inputFile = path.resolve(`${__dirname}/samples/${dir}/input.svelte`);
const outputBase = path.resolve(`${__dirname}/samples/${dir}/_actual`);
const preprocessorFilename = path.resolve(
`${__dirname}/samples/${dir}/_preprocessor.js`
)
const input = {};
input.code = fs.readFileSync(inputFile, "utf-8");
input.locate = getLocator(input.code);
const input = fs.readFileSync(filename, "utf-8").replace(/\s+$/, "");
let processed_input = input;
let processed_map = null;
const preprocessed = await svelte.preprocess(
input.code,
config.preprocess, {
filename: "input.svelte"
});
if (fs.existsSync(preprocessorFilename)) {
let { preprocessors } = require(preprocessorFilename);
if (preprocessors.length > 0) {
({ code: processed_input, map: processed_map } = await svelte.preprocess(input, preprocessors, { filename: 'input.svelte' }));
}
}
const { js, css } = svelte.compile(processed_input, {
filename,
sourcemap: processed_map,
outputFilename: `${outputFilename}.js`,
cssOutputFilename: `${outputFilename}.css`
const { js, css } = svelte.compile(
preprocessed.code, {
filename: "input.svelte",
sourcemap: preprocessed.map,
// filenames for sourcemaps
outputFilename: "output.js",
cssOutputFilename: "output.css",
});
const _code = js.code.replace(/Svelte v\d+\.\d+\.\d+/, match => match.replace(/\d/g, 'x'));
js.code = js.code.replace(
/generated by Svelte v\d+\.\d+\.\d+/,
match => match.replace(/\d/g, "x")
);
fs.writeFileSync(`${outputBase}.html`, preprocessed.code);
if (preprocessed.map) {
fs.writeFileSync(`${outputBase}.html.map`, JSON.stringify(preprocessed.map, null, 2));
}
fs.writeFileSync(
`${outputFilename}.js`,
`${_code}\n//# sourceMappingURL=output.js.map`
`${outputBase}.js`,
`${js.code}\n//# sourceMappingURL=output.js.map`
);
fs.writeFileSync(
`${outputFilename}.js.map`,
JSON.stringify(js.map, null, " ")
`${outputBase}.js.map`,
JSON.stringify(js.map, 0, 2)
);
if (css.code) {
fs.writeFileSync(
`${outputFilename}.css`,
`${outputBase}.css`,
`${css.code}\n/*# sourceMappingURL=output.css.map */`
);
fs.writeFileSync(
`${outputFilename}.css.map`,
JSON.stringify(css.map, null, " ")
`${outputBase}.css.map`,
JSON.stringify(css.map, 0, 2)
);
}
assert.deepEqual(js.map.sources, ["input.svelte"]);
if (css.map) assert.deepEqual(css.map.sources, ["input.svelte"]);
const { test } = require(`./samples/${dir}/test.js`);
const locateInSource = getLocator(input);
js.mapConsumer = await new SourceMapConsumer(js.map);
js.locate = getLocator(js.code);
const smc = await new SourceMapConsumer(js.map);
const locateInGenerated = getLocator(_code);
css.mapConsumer = css.map && await new SourceMapConsumer(css.map);
css.locate = getLocator(css.code || "");
const smcCss = css.map && await new SourceMapConsumer(css.map);
const locateInGeneratedCss = getLocator(css.code || '');
test({ assert, input, js, css });
test({ assert, code: _code, map: js.map, smc, smcCss, locateInSource, locateInGenerated, locateInGeneratedCss });
});
});
});

@ -1,12 +1,12 @@
export function test({ assert, smc, locateInSource, locateInGenerated }) {
const expected = locateInSource('foo.bar.baz');
export function test({ assert, input, js }) {
const expected = input.locate('foo.bar.baz');
let start;
let actual;
start = locateInGenerated('ctx[0].bar.baz');
start = js.locate('ctx[0].bar.baz');
actual = smc.originalPositionFor({
actual = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
@ -18,9 +18,9 @@ export function test({ assert, smc, locateInSource, locateInGenerated }) {
column: expected.column
});
start = locateInGenerated('ctx[0].bar.baz', start.character + 1);
start = js.locate('ctx[0].bar.baz', start.character + 1);
actual = smc.originalPositionFor({
actual = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});

@ -1,13 +1,14 @@
export function test({ assert, smc, locateInSource, locateInGenerated }) {
const expected = locateInSource('potato');
export function test({ assert, input, js }) {
const expected = input.locate('potato');
let start;
start = locateInGenerated('potato');
start = locateInGenerated('potato', start.character + 1);
start = locateInGenerated('potato', start.character + 1); // we need the third instance of 'potato'
start = js.locate('potato');
start = js.locate('potato', start.character + 1);
start = js.locate('potato', start.character + 1);
// we need the third instance of 'potato'
const actual = smc.originalPositionFor({
const actual = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});

@ -1,12 +1,12 @@
export function test({ assert, smc, locateInSource, locateInGenerated }) {
const expected = locateInSource('bar.baz');
export function test({ assert, input, js }) {
const expected = input.locate('bar.baz');
let start;
let actual;
start = locateInGenerated('bar.baz');
start = js.locate('bar.baz');
actual = smc.originalPositionFor({
actual = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
@ -18,9 +18,9 @@ export function test({ assert, smc, locateInSource, locateInGenerated }) {
column: expected.column
});
start = locateInGenerated('bar.baz', start.character + 1);
start = js.locate('bar.baz', start.character + 1);
actual = smc.originalPositionFor({
actual = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});

@ -4,4 +4,4 @@
.foo {
color: red;
}
</style>
</style>

@ -1,14 +1,14 @@
export function test({ assert, smcCss, locateInSource, locateInGeneratedCss }) {
const expected = locateInSource( '.foo' );
export function test({ assert, input, css }) {
const expected = input.locate('.foo');
const start = locateInGeneratedCss( '.foo' );
const start = css.locate('.foo');
const actual = smcCss.originalPositionFor({
const actual = css.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
assert.deepEqual( actual, {
assert.deepEqual(actual, {
source: 'input.svelte',
name: null,
line: expected.line + 1,

@ -1,3 +1,3 @@
{#each foo as bar}
<span>{bar}</span>
{/each}
{/each}

@ -1,10 +1,10 @@
export function test({ assert, code, smc, map, locateInSource, locateInGenerated }) {
const startIndex = code.indexOf('create_main_fragment');
export function test({ assert, input, js }) {
const startIndex = js.code.indexOf('create_main_fragment');
const expected = locateInSource('each');
const start = locateInGenerated('length', startIndex);
const expected = input.locate('each');
const start = js.locate('length', startIndex);
const actual = smc.originalPositionFor({
const actual = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});

@ -1,16 +1,18 @@
import MagicString from 'magic-string';
export const preprocessors = [{
markup: ({content, filename}) => {
const src = new MagicString(content);
const idx = content.indexOf("baritone");
src.overwrite(idx, idx+"baritone".length, "bar");
return {
code: src.toString(),
map: src.generateMap({
source: filename,
includeContent: false
})
};
}
}];
export default {
preprocess: {
markup: ({ content, filename }) => {
const src = new MagicString(content);
const idx = content.indexOf("baritone");
src.overwrite(idx, idx+"baritone".length, "bar");
return {
code: src.toString(),
map: src.generateMap({
source: filename,
includeContent: false
})
};
}
}
};

@ -1,10 +1,10 @@
export function test({ assert, smc, locateInSource, locateInGenerated }) {
const expectedBar = locateInSource('baritone.baz');
const expectedBaz = locateInSource('.baz');
export function test({ assert, input, js }) {
const expectedBar = input.locate('baritone.baz');
const expectedBaz = input.locate('.baz');
let start = locateInGenerated('bar.baz');
let start = js.locate('bar.baz');
const actualbar = smc.originalPositionFor({
const actualbar = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
@ -16,9 +16,9 @@ export function test({ assert, smc, locateInSource, locateInGenerated }) {
column: expectedBar.column
});
start = locateInGenerated('.baz');
start = js.locate('.baz');
const actualbaz = smc.originalPositionFor({
const actualbaz = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});

@ -1,51 +1,48 @@
import MagicString from 'magic-string';
export const preprocessors = [{
markup: ({ content, filename }) => {
const src = new MagicString(content);
const idx = content.indexOf("baritone");
src.overwrite(idx, idx + "baritone".length, "bar");
export default {
preprocess: {
markup: ({ content, filename }) => {
const src = new MagicString(content);
const idx = content.indexOf("baritone");
src.overwrite(idx, idx + "baritone".length, "bar");
const css_idx = content.indexOf("--bazitone");
src.overwrite(css_idx, css_idx + "--bazitone".length, "--baz");
return {
code: src.toString(),
map: src.generateMap({
source: filename,
hires: true,
includeContent: false
})
};
}
},
{
script: ({ content, filename }) => {
const src = new MagicString(content);
const idx = content.indexOf("bar");
src.prependLeft(idx, " ");
return {
code: src.toString(),
map: src.generateMap({
source: filename,
hires: true,
includeContent: false
})
};
}
},
{
style: ({ content, filename }) => {
const src = new MagicString(content);
const idx = content.indexOf("--baz");
src.prependLeft(idx, " ");
return {
code: src.toString(),
map: src.generateMap({
source: filename,
hires: true,
includeContent: false
})
};
}
}
];
const css_idx = content.indexOf("--bazitone");
src.overwrite(css_idx, css_idx + "--bazitone".length, "--baz");
return {
code: src.toString(),
map: src.generateMap({
source: filename,
hires: true,
includeContent: false
})
};
},
script: ({ content, filename }) => {
const src = new MagicString(content);
const idx = content.indexOf("bar");
src.prependLeft(idx, " ");
return {
code: src.toString(),
map: src.generateMap({
source: filename,
hires: true,
includeContent: false
})
};
},
style: ({ content, filename }) => {
const src = new MagicString(content);
const idx = content.indexOf("--baz");
src.prependLeft(idx, " ");
return {
code: src.toString(),
map: src.generateMap({
source: filename,
hires: true,
includeContent: false
})
};
}
}
};

@ -1,10 +1,10 @@
export function test({ assert, smc, smcCss, locateInSource, locateInGenerated, locateInGeneratedCss }) {
const expectedBar = locateInSource('baritone');
const expectedBaz = locateInSource('--bazitone');
let start = locateInGenerated('bar');
export function test({ assert, input, js, css }) {
const expectedBar = input.locate('baritone');
const expectedBaz = input.locate('--bazitone');
const actualbar = smc.originalPositionFor({
let start = js.locate('bar');
const actualbar = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
@ -16,9 +16,9 @@ export function test({ assert, smc, smcCss, locateInSource, locateInGenerated, l
column: expectedBar.column
});
start = locateInGeneratedCss('--baz');
start = css.locate('--baz');
const actualbaz = smcCss.originalPositionFor({
const actualbaz = css.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
@ -28,5 +28,10 @@ export function test({ assert, smc, smcCss, locateInSource, locateInGenerated, l
name: null,
line: expectedBaz.line + 1,
column: expectedBaz.column
}, `couldn't find baz in css,\n gen:${JSON.stringify(start)}\n actual:${JSON.stringify(actualbaz)}\n expected:${JSON.stringify(expectedBaz)}`);
}, `\
couldn't find baz in css,
gen: ${JSON.stringify(start)}
actual: ${JSON.stringify(actualbaz)}
expected: ${JSON.stringify(expectedBaz)}\
`);
}

@ -1,17 +1,19 @@
import MagicString from 'magic-string';
export const preprocessors = [{
script: ({content, filename}) => {
const src = new MagicString(content);
const idx = content.indexOf("baritone");
src.overwrite(idx, idx+"baritone".length, "bar");
return {
code: src.toString(),
map: src.generateMap({
source: filename,
hires: true,
includeContent: false
})
};
}
}];
export default {
preprocess: {
script: ({ content, filename }) => {
const src = new MagicString(content);
const idx = content.indexOf("baritone");
src.overwrite(idx, idx+"baritone".length, "bar");
return {
code: src.toString(),
map: src.generateMap({
source: filename,
hires: true,
includeContent: false
})
};
}
}
};

@ -1,10 +1,10 @@
export function test({ assert, smc, locateInSource, locateInGenerated }) {
const expectedBar = locateInSource('baritone:');
const expectedBaz = locateInSource('baz:');
export function test({ assert, input, js }) {
const expectedBar = input.locate('baritone:');
const expectedBaz = input.locate('baz:');
let start = locateInGenerated('bar:');
let start = js.locate('bar:');
const actualbar = smc.originalPositionFor({
const actualbar = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
@ -14,11 +14,11 @@ export function test({ assert, smc, locateInSource, locateInGenerated }) {
name: null,
line: expectedBar.line + 1,
column: expectedBar.column
}, `couldn't find bar: in source` );
}, "couldn't find bar: in source");
start = locateInGenerated('baz:');
start = js.locate('baz:');
const actualbaz = smc.originalPositionFor({
const actualbaz = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
@ -28,5 +28,5 @@ export function test({ assert, smc, locateInSource, locateInGenerated }) {
name: null,
line: expectedBaz.line + 1,
column: expectedBaz.column
}, `couldn't find baz: in source` );
}, "couldn't find baz: in source");
}

@ -1,17 +1,19 @@
import MagicString from 'magic-string';
export const preprocessors = [{
style: ({content, filename}) => {
const src = new MagicString(content);
const idx = content.indexOf("baritone");
src.overwrite(idx, idx+"baritone".length, "bar");
return {
code: src.toString(),
map: src.generateMap({
source: filename,
hires: true,
includeContent: false
})
};
}
}];
export default {
preprocess: {
style: ({ content, filename }) => {
const src = new MagicString(content);
const idx = content.indexOf("baritone");
src.overwrite(idx, idx+"baritone".length, "bar");
return {
code: src.toString(),
map: src.generateMap({
source: filename,
hires: true,
includeContent: false
})
};
}
}
};

@ -1,10 +1,10 @@
export function test({ assert, smcCss, locateInSource, locateInGeneratedCss }) {
const expectedBar = locateInSource('--baritone');
const expectedBaz = locateInSource('--baz');
let start = locateInGeneratedCss('--bar');
export function test({ assert, input, css }) {
const expectedBar = input.locate('--baritone');
const expectedBaz = input.locate('--baz');
const actualbar = smcCss.originalPositionFor({
let start = css.locate('--bar');
const actualbar = css.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
@ -14,11 +14,11 @@ export function test({ assert, smcCss, locateInSource, locateInGeneratedCss }) {
name: null,
line: expectedBar.line + 1,
column: expectedBar.column
}, `couldn't find bar in source` );
}, "couldn't find bar in source");
start = locateInGeneratedCss('--baz');
start = css.locate('--baz');
const actualbaz = smcCss.originalPositionFor({
const actualbaz = css.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
@ -28,5 +28,5 @@ export function test({ assert, smcCss, locateInSource, locateInGeneratedCss }) {
name: null,
line: expectedBaz.line + 1,
column: expectedBaz.column
}, `couldn't find baz in source` );
}, "couldn't find baz in source");
}

@ -1,13 +1,13 @@
export function test({ assert, smc, locateInSource, locateInGenerated }) {
const expected = locateInSource( 'assertThisLine' );
const start = locateInGenerated( 'assertThisLine' );
export function test({ assert, input, js }) {
const expected = input.locate('assertThisLine');
const start = js.locate('assertThisLine');
const actual = smc.originalPositionFor({
const actual = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
assert.deepEqual( actual, {
assert.deepEqual(actual, {
source: 'input.svelte',
name: null,
line: expected.line + 1,

@ -1,8 +1,8 @@
export function test({ assert, smc, locateInSource, locateInGenerated }) {
const expected = locateInSource( '42' );
const start = locateInGenerated( '42' );
export function test({ assert, input, js }) {
const expected = input.locate('42');
const start = js.locate('42');
const actual = smc.originalPositionFor({
const actual = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});

@ -1 +1 @@
<p>no moving parts</p>
<p>no moving parts</p>

@ -1,9 +1,9 @@
const fs = require( 'fs' );
const path = require( 'path' );
const fs = require('fs');
const path = require('path');
export function test({ assert, map }) {
assert.deepEqual( map.sources, [ 'input.svelte' ]);
assert.deepEqual( map.sourcesContent, [
fs.readFileSync( path.join( __dirname, 'input.svelte' ), 'utf-8' )
export function test({ assert, js }) {
assert.deepEqual(js.map.sources, ['input.svelte']);
assert.deepEqual(js.map.sourcesContent, [
fs.readFileSync(path.join(__dirname, 'input.svelte'), 'utf-8')
]);
}

@ -1,8 +1,8 @@
export function test({ assert, smc, locateInSource, locateInGenerated }) {
const expected = locateInSource( 'assertThisLine' );
const start = locateInGenerated( 'assertThisLine' );
export function test({ assert, input, js }) {
const expected = input.locate( 'assertThisLine' );
const start = js.locate( 'assertThisLine' );
const actual = smc.originalPositionFor({
const actual = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});

Loading…
Cancel
Save