Skip to content

Commit

Permalink
test: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Sep 28, 2020
1 parent 8afed41 commit 5d3c8bb
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 68 deletions.
20 changes: 11 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ function getStylusOptions(loaderContext, loaderOptions) {
return stylusOptions;
}

async function runGlob(patterns, options) {
const paths = await fastGlob(patterns, { absolute: true, ...options });

return paths.sort().filter((file) => /\.styl$/i.test(file));
}

async function resolveFilename(
loaderContext,
webpackFileResolver,
Expand Down Expand Up @@ -61,15 +67,11 @@ async function resolveFilename(
if (isGlob && result) {
loaderContext.addContextDependency(result);

return (
await fastGlob(
// TODO more test and improve it
parsedGlob.patterns.map((item) =>
item.slice(parsedGlob.base.length + 1)
),
{ cwd: result, absolute: true }
)
).filter((file) => /\.styl$/i.test(file));
const patterns = parsedGlob.patterns.map((item) =>
item.slice(parsedGlob.base.length + 1)
);

return runGlob(patterns, { cwd: result });
}

return result;
Expand Down
57 changes: 57 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,41 @@ exports[`loader imports files listed in glob with deps: errors 1`] = `Array []`;

exports[`loader imports files listed in glob with deps: warnings 1`] = `Array []`;

exports[`loader imports files listed in glob with webpack import 2: css 1`] = `
".a-glob {
color: #000;
}
.b-glob {
background: #808080;
}
.a-glob-webpack {
color: #000;
}
.b-glob-webpack {
background: #808080;
}
.a-glob {
color: #000;
}
.b-glob {
background: #808080;
}
.a-glob-webpack {
color: #000;
}
.b-glob-webpack {
background: #808080;
}
.glob-entry {
box-sizing: border-box;
}
"
`;

exports[`loader imports files listed in glob with webpack import 2: errors 1`] = `Array []`;

exports[`loader imports files listed in glob with webpack import 2: warnings 1`] = `Array []`;

exports[`loader imports files listed in glob with webpack import with deps: css 1`] = `
".a-glob {
color: #000;
Expand Down Expand Up @@ -328,6 +363,28 @@ exports[`loader imports files listed in option as glob with webpack import: erro

exports[`loader imports files listed in option as glob with webpack import: warnings 1`] = `Array []`;

exports[`loader imports files listed in option as glob: css 1`] = `
".a-glob {
color: #000;
}
.b-glob {
background: #808080;
}
body {
font: 12px Helvetica, Arial, sans-serif;
}
a.button {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
"
`;

exports[`loader imports files listed in option as glob: errors 1`] = `Array []`;

exports[`loader imports files listed in option as glob: warnings 1`] = `Array []`;

exports[`loader imports files with special characters listed in glob: css 1`] = `
".directoryfile {
color: #f00;
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/import-webpack-dir-like-a-glob.styl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import 'like-a-glob*'
@import '~like-a-glob*'

.entry
box-sizing: border-box
2 changes: 1 addition & 1 deletion test/helpers/getCodeFromStylus.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const pathMap = {
'~aliasNested/**/file.styl': 'glob-nested/**/file.styl',
'~globAliasDot/*': 'glob-webpack-2/*',
'glob_package/*': 'node_modules/glob_package/*',
'like-a-glob*': 'node_modules/like-a-glob*',
'~like-a-glob*': 'node_modules/like-a-glob*',
'alias/1': path.resolve(fixturesDir, 'alias', '1.styl'),
'~alias/2': path.resolve(fixturesDir, 'alias', '2.styl'),
'globAlias/*': path.resolve(fixturesDir, 'glob-webpack/*'),
Expand Down
116 changes: 59 additions & 57 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
validateDependencies,
} from './helpers';

jest.setTimeout(30000);

describe('loader', () => {
it('should work', async () => {
const testId = './basic.styl';
Expand Down Expand Up @@ -948,7 +950,7 @@ describe('loader', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it.skip('imports files listed in glob with webpack import 2', async () => {
it('imports files listed in glob with webpack import 2', async () => {
const testId = './import-glob-webpack-2.styl';
const compiler = getCompiler(
testId,
Expand All @@ -957,6 +959,7 @@ describe('loader', () => {
resolve: {
alias: {
globAliasDot: path.resolve(__dirname, 'fixtures', 'glob-webpack-2'),
globAlias2: path.resolve(__dirname, 'fixtures', 'glob'),
},
},
}
Expand Down Expand Up @@ -1048,75 +1051,74 @@ describe('loader', () => {

if (isWin) {
expect(true).toBe(true);
}
} else {
const rootdir = path.resolve(__dirname, 'fixtures', 'node_modules');
const exampleDir = path.resolve(rootdir, 'like-a-glob-example');
const pathDir = path.resolve(rootdir, 'like-a-glob*');

if (!fs.existsSync(pathDir)) {
fs.mkdirSync(pathDir);
fs.copyFileSync(
path.resolve(exampleDir, 'package.json'),
path.resolve(pathDir, 'package.json')
);
fs.copyFileSync(
path.resolve(exampleDir, 'index.styl'),
path.resolve(pathDir, 'index.styl')
);
}

const rootdir = path.resolve(__dirname, 'fixtures', 'node_modules');
const exampleDir = path.resolve(rootdir, 'like-a-glob-example');
const pathDir = path.resolve(rootdir, 'like-a-glob*');

if (!fs.existsSync(pathDir)) {
fs.mkdirSync(pathDir);
fs.copyFileSync(
path.resolve(exampleDir, 'package.json'),
path.resolve(pathDir, 'package.json')
);
fs.copyFileSync(
path.resolve(exampleDir, 'index.styl'),
path.resolve(pathDir, 'index.styl')
);
const testId = './import-dir-like-a-glob.styl';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromStylus = await getCodeFromStylus(testId);

// Native stylus incorrectly identifies the directory id directory like a glob
expect(codeFromBundle.css).toBe(codeFromStylus.css);
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
}

const testId = './import-dir-like-a-glob.styl';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
// const codeFromStylus = await getCodeFromStylus(testId);

// Native stylus incorrectly identifies the directory id directory like a glob
// expect(codeFromBundle.css).toBe(codeFromStylus.css);
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it.skip('imports files in dir like a glob through webpack', async () => {
const isWin = process.platform === 'win32';

if (isWin) {
expect(true).toBe(true);
return;
}
} else {
const rootdir = path.resolve(__dirname, 'fixtures', 'node_modules');
const exampleDir = path.resolve(rootdir, 'like-a-glob-example');
const pathDir = path.resolve(rootdir, 'like-a-glob*');

if (!fs.existsSync(pathDir)) {
fs.mkdirSync(pathDir);
fs.copyFileSync(
path.resolve(exampleDir, 'package.json'),
path.resolve(pathDir, 'package.json')
);
fs.copyFileSync(
path.resolve(exampleDir, 'index.styl'),
path.resolve(pathDir, 'index.styl')
);
}

const rootdir = path.resolve(__dirname, 'fixtures', 'node_modules');
const exampleDir = path.resolve(rootdir, 'like-a-glob-example');
const pathDir = path.resolve(rootdir, 'like-a-glob*');

if (!fs.existsSync(pathDir)) {
fs.mkdirSync(pathDir);
fs.copyFileSync(
path.resolve(exampleDir, 'package.json'),
path.resolve(pathDir, 'package.json')
);
fs.copyFileSync(
path.resolve(exampleDir, 'index.styl'),
path.resolve(pathDir, 'index.styl')
);
const testId = './import-webpack-dir-like-a-glob.styl';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromStylus = await getCodeFromStylus(testId);

// Native stylus incorrectly identifies the directory id directory like a glob
expect(codeFromBundle.css).toBe(codeFromStylus.css);
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
}

const testId = './import-webpack-dir-like-a-glob.styl';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
// const codeFromStylus = await getCodeFromStylus(testId);

// Native stylus incorrectly identifies the directory id directory like a glob
// expect(codeFromBundle.css).toBe(codeFromStylus.css);
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it.skip('imports files listed in option as glob', async () => {
it('imports files listed in option as glob', async () => {
const testId = './basic.styl';
const compiler = getCompiler(testId, {
stylusOptions: {
Expand Down

0 comments on commit 5d3c8bb

Please sign in to comment.