generated from voxpelli/node-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Because of voxpelli/node-test-pretty-reporter#6
- Loading branch information
Showing
6 changed files
with
161 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,110 @@ | ||
import { describe, it } from 'node:test'; | ||
|
||
import chai from 'chai'; | ||
import chaiPosixPath from 'chai-posix-path'; | ||
import assert, { AssertionError } from 'node:assert/strict'; | ||
|
||
import { normalizePluginName } from '../lib/plugin-importer/normalize-plugin-name.js'; | ||
|
||
chai.use(chaiPosixPath); | ||
/** | ||
* @param {string} sourcePath | ||
* @param {string} targetPath | ||
* @returns {void} | ||
*/ | ||
function assertPosixPath (sourcePath, targetPath) { | ||
const posixPath = sourcePath.split('\\').join('/'); | ||
|
||
const should = chai.should(); | ||
if (posixPath !== targetPath) { | ||
throw new AssertionError({ | ||
message: `expected ${sourcePath} to translate to posix path ${targetPath} but translated to ${posixPath}`, | ||
actual: posixPath, | ||
expected: targetPath, | ||
}); | ||
} | ||
} | ||
|
||
describe('Normalize Plugin Name', () => { | ||
describe('normalizePluginName()', () => { | ||
it('should require a substantive string pluginName argument', () => { | ||
// @ts-ignore | ||
should.Throw(() => { normalizePluginName(); }, TypeError, 'Invalid pluginName, expected a string'); | ||
assert.throws(() => { normalizePluginName(); }, { | ||
name: 'TypeError', | ||
message: 'Invalid pluginName, expected a string', | ||
}); | ||
// @ts-ignore | ||
should.Throw(() => { normalizePluginName(123); }, TypeError, 'Invalid pluginName, expected a string'); | ||
assert.throws(() => { normalizePluginName(123); }, { | ||
name: 'TypeError', | ||
message: 'Invalid pluginName, expected a string', | ||
}); | ||
|
||
should.Throw(() => { normalizePluginName(''); }, Error, 'Invalid pluginName, expected the string to be non-empty'); | ||
assert.throws(() => { normalizePluginName(''); }, { | ||
name: 'Error', | ||
message: 'Invalid pluginName, expected the string to be non-empty', | ||
}); | ||
|
||
should.Throw(() => { normalizePluginName(' '); }, Error, 'Invalid pluginName, expected the string to not begin or end with whitespace'); | ||
should.Throw(() => { normalizePluginName('foo '); }, Error, 'Invalid pluginName, expected the string to not begin or end with whitespace'); | ||
should.Throw(() => { normalizePluginName(' foo'); }, Error, 'Invalid pluginName, expected the string to not begin or end with whitespace'); | ||
should.Throw(() => { normalizePluginName(' foo '); }, Error, 'Invalid pluginName, expected the string to not begin or end with whitespace'); | ||
assert.throws(() => { normalizePluginName(' '); }, { | ||
name: 'Error', | ||
message: 'Invalid pluginName, expected the string to not begin or end with whitespace', | ||
}); | ||
assert.throws(() => { normalizePluginName('foo '); }, { | ||
name: 'Error', | ||
message: 'Invalid pluginName, expected the string to not begin or end with whitespace', | ||
}); | ||
assert.throws(() => { normalizePluginName(' foo'); }, { | ||
name: 'Error', | ||
message: 'Invalid pluginName, expected the string to not begin or end with whitespace', | ||
}); | ||
assert.throws(() => { normalizePluginName(' foo '); }, { | ||
name: 'Error', | ||
message: 'Invalid pluginName, expected the string to not begin or end with whitespace', | ||
}); | ||
}); | ||
|
||
it('should throw on upwards directory traversing', () => { | ||
should.not.Throw(() => { normalizePluginName('./foo/../bar/'); }); | ||
should.Throw(() => { normalizePluginName('./foo/../../bar/'); }, 'Plugin name attempts directory traversal: "./foo/../../bar/"'); | ||
assert.doesNotThrow(() => { normalizePluginName('./foo/../bar/'); }); | ||
assert.throws(() => { normalizePluginName('./foo/../../bar/'); }, { | ||
message: 'Plugin name attempts directory traversal: "./foo/../../bar/"', | ||
}); | ||
}); | ||
|
||
it('should return simple plugin name unaltered', () => { | ||
const result = normalizePluginName('foo'); | ||
should.exist(result); | ||
result.should.equal('foo'); | ||
assert.ok(result); | ||
assert.strictEqual(result, 'foo'); | ||
}); | ||
|
||
it('should add prefix when one is set', () => { | ||
const result = normalizePluginName('foo', 'example-prefix'); | ||
should.exist(result); | ||
result.should.equal('example-prefix-foo'); | ||
assert.ok(result); | ||
assert.strictEqual(result, 'example-prefix-foo'); | ||
}); | ||
|
||
it('should add prefix as suffix to plain scopes', () => { | ||
const result = normalizePluginName('@foo', 'example-prefix'); | ||
should.exist(result); | ||
result.should.equal('@foo/example-prefix'); | ||
assert.ok(result); | ||
assert.strictEqual(result, '@foo/example-prefix'); | ||
}); | ||
|
||
it('should not add prefix if already prefixed', () => { | ||
const result = normalizePluginName('example-prefix-foo', 'example-prefix'); | ||
should.exist(result); | ||
result.should.equal('example-prefix-foo'); | ||
assert.ok(result); | ||
assert.strictEqual(result, 'example-prefix-foo'); | ||
}); | ||
|
||
it('should return local path without prefix but normalized', () => { | ||
normalizePluginName('./foo/../bar/', 'example-prefix') | ||
.should.be.posixPath('./bar/'); | ||
normalizePluginName('./foo', 'example-prefix') | ||
.should.be.posixPath('./foo'); | ||
normalizePluginName('./foo/../bar/') | ||
.should.be.posixPath('./bar/'); | ||
normalizePluginName('./foo') | ||
.should.be.posixPath('./foo'); | ||
assertPosixPath( | ||
normalizePluginName('./foo/../bar/', 'example-prefix'), | ||
'./bar/' | ||
); | ||
assertPosixPath( | ||
normalizePluginName('./foo', 'example-prefix'), | ||
'./foo' | ||
); | ||
assertPosixPath( | ||
normalizePluginName('./foo/../bar/'), | ||
'./bar/' | ||
); | ||
assertPosixPath( | ||
normalizePluginName('./foo'), | ||
'./foo' | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.