Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Jan 13, 2017
1 parent becbea2 commit c198c38
Show file tree
Hide file tree
Showing 39 changed files with 295 additions and 80 deletions.
22 changes: 8 additions & 14 deletions rules/filename-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,20 @@ const fixFilename = (chosenCase, filename) => filename
.map(ignoreNumbers(chosenCase.fn))
.join('.');

const leadingUnserscoresRegex = /^(_+)(.*)$/;
const splitFilename = filename => {
const res = leadingUnserscoresRegex.exec(filename);
return {
leading: (res && res[1]) || '',
trailing: (res && res[2]) || filename
};
};

module.exports = ctx => {
const chosenCase = cases[ctx.options[0].case || 'camelCase'];
let chosenCase = cases.camelCase;

if (ctx.options[0] && ctx.options[0].case) {
chosenCase = cases[ctx.options[0].case];
}

for (const file of ctx.files) {
const extension = path.extname(file);
const filename = path.basename(file, extension);
const splitName = splitFilename(filename);
const fixedFilename = fixFilename(chosenCase, splitName.trailing);
const renameFilename = splitName.leading + fixedFilename + extension;
const fixedFilename = fixFilename(chosenCase, filename);
const renameFilename = fixedFilename + extension;

if (fixedFilename !== splitName.trailing) {
if (fixedFilename !== filename) {
ctx.report({
message: `Filename is not in ${chosenCase.name}. Rename it to \`${renameFilename}\`.`,
file: ctx.fs.resolve(file)
Expand Down
4 changes: 2 additions & 2 deletions rules/test-script.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
module.exports = ctx => ctx.fs.readFile('package.json').then(pkg => {
if (pkg.scripts && (/no test specified/.test(pkg.scripts.test) || pkg.scripts.test === '')) {
if (pkg.scripts && (!pkg.scripts.test || /no test specified/.test(pkg.scripts.test))) {
ctx.report({
message: 'The package is untested'
message: 'The package is untested.'
});
}
});
4 changes: 1 addition & 3 deletions rules/travis.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ module.exports = ctx => {
message: 'Language is not set to `node_js`.',
file
});
}

if (!travis.node_js) {
} else if (!travis.node_js) {
ctx.report({
message: 'No Node.js versions specified.',
file
Expand Down
25 changes: 25 additions & 0 deletions test/ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,31 @@ test('test script', async t => {
);
});

test('ava is not part of the test script', async t => {
await ruleTester(t, 'xo',
[
{
ruleId: 'ava',
severity: 'error',
message: 'AVA is not used in the test script.',
file: path.resolve(opts.cwd, 'xo/package.json')
}
],
[
{
name: 'ava',
scripts: {
test: 'xo && ava'
},
devDependencies: {
ava: '*',
xo: '*'
}
}
]
);
});

test('cli config', async t => {
await ruleTester(t, 'cli-config',
[
Expand Down
4 changes: 4 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ test('handle `bin` object', async t => {
]
);
});

test('no `bin` property', async t => {
await ruleTester(t, '..', []);
});
56 changes: 49 additions & 7 deletions test/filename-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,64 @@ import test from 'ava';
import clintonRuleTester from './fixtures/rule-tester';

const opts = {
cwd: 'test/fixtures/filename-case',
rules: {
'filename-case': ['error', {case: 'kebabCase'}]
}
cwd: 'test/fixtures/filename-case'
};

const ruleTester = clintonRuleTester(opts);

test('wrong casing', async t => {
await ruleTester(t, '.',
test('falls back to camelcase', async t => {
await ruleTester(t, 'default',
[
{
ruleId: 'filename-case',
severity: 'error',
message: 'Filename is not in camel case. Rename it to `fooBar.js`.',
file: path.resolve(opts.cwd, 'default/foo_bar.js')
}
]
);
});

test('kebab casing', async t => {
await ruleTester(t, 'kebab',
[
{
ruleId: 'filename-case',
severity: 'error',
message: 'Filename is not in kebab case. Rename it to `readme.md`.',
file: path.resolve(opts.cwd, 'README.md')
file: path.resolve(opts.cwd, 'kebab/README.md')
}
]
);
});

test('snake casing', async t => {
await ruleTester(t, 'snake',
[
{
ruleId: 'filename-case',
severity: 'error',
message: 'Filename is not in snake case. Rename it to `foo_bar.js`.',
file: path.resolve(opts.cwd, 'snake/fooBar.js')
}
]
);
});

test('pascal casing', async t => {
await ruleTester(t, 'pascal',
[
{
ruleId: 'filename-case',
severity: 'error',
message: 'Filename is not in pascal case. Rename it to `FooBar.js`.',
file: path.resolve(opts.cwd, 'pascal/fooBar.js')
},
{
ruleId: 'filename-case',
severity: 'error',
message: 'Filename is not in pascal case. Rename it to `Package.json`.',
file: path.resolve(opts.cwd, 'pascal/package.json')
}
]
);
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/ava/xo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "ava",
"scripts": {
"test": "xo"
},
"devDependencies": {
"ava": "*",
"xo": "*"
}
}
1 change: 1 addition & 0 deletions test/fixtures/filename-case/default/foo_bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('foo bar');
8 changes: 8 additions & 0 deletions test/fixtures/filename-case/default/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "filename-case",
"clinton": {
"rules": {
"filename-case": ["error"]
}
}
}
1 change: 1 addition & 0 deletions test/fixtures/filename-case/kebab/5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('foo bar');
File renamed without changes.
8 changes: 8 additions & 0 deletions test/fixtures/filename-case/kebab/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "filename-case",
"clinton": {
"rules": {
"filename-case": ["error", {"case": "kebabCase"}]
}
}
}
3 changes: 0 additions & 3 deletions test/fixtures/filename-case/package.json

This file was deleted.

1 change: 1 addition & 0 deletions test/fixtures/filename-case/pascal/fooBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('foo bar');
8 changes: 8 additions & 0 deletions test/fixtures/filename-case/pascal/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "filename-case",
"clinton": {
"rules": {
"filename-case": ["error", {"case": "pascalCase"}]
}
}
}
1 change: 1 addition & 0 deletions test/fixtures/filename-case/snake/fooBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('foo bar');
8 changes: 8 additions & 0 deletions test/fixtures/filename-case/snake/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "filename-case",
"clinton": {
"rules": {
"filename-case": ["error", {"case": "snakeCase"}]
}
}
}
3 changes: 3 additions & 0 deletions test/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "empty"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@
"repository": {
"type": "git",
"url": "https://bitbucket.com/SamVerschueren/clinton.git"
},
"clinton": {
"rules": {
"pkg-shorthand-repository": "error"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
{
"name": "package",
"repository": "https://bitbucket.com/SamVerschueren/clinton.git",
"clinton": {
"rules": {
"pkg-shorthand-repository": "error"
}
}
"repository": "https://bitbucket.com/SamVerschueren/clinton.git"
}
5 changes: 0 additions & 5 deletions test/fixtures/pkg-shorthand-repo/git-object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@
"repository": {
"type": "git",
"url": "https://github.com/SamVerschueren/clinton.git"
},
"clinton": {
"rules": {
"pkg-shorthand-repository": "error"
}
}
}
7 changes: 1 addition & 6 deletions test/fixtures/pkg-shorthand-repo/git-string/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
{
"name": "package",
"repository": "https://github.com/SamVerschueren/clinton.git",
"clinton": {
"rules": {
"pkg-shorthand-repository": "error"
}
}
"repository": "https://github.com/SamVerschueren/clinton.git"
}
5 changes: 0 additions & 5 deletions test/fixtures/pkg-shorthand-repo/object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@
"repository": {
"type": "git",
"url": "https://github.com/SamVerschueren/clinton"
},
"clinton": {
"rules": {
"pkg-shorthand-repository": "error"
}
}
}
7 changes: 1 addition & 6 deletions test/fixtures/pkg-shorthand-repo/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
{
"name": "package",
"repository": "SamVerschueren/clinton",
"clinton": {
"rules": {
"pkg-shorthand-repository": "error"
}
}
"repository": "SamVerschueren/clinton"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@
"repository": {
"type": "git",
"url": "SamVerschueren/clinton"
},
"clinton": {
"rules": {
"pkg-shorthand-repository": "error"
}
}
}
7 changes: 1 addition & 6 deletions test/fixtures/pkg-shorthand-repo/string/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
{
"name": "package",
"repository": "https://github.com/SamVerschueren/clinton",
"clinton": {
"rules": {
"pkg-shorthand-repository": "error"
}
}
"repository": "https://github.com/SamVerschueren/clinton"
}
6 changes: 6 additions & 0 deletions test/fixtures/test-script/empty-test-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test-script",
"scripts": {
"test": ""
}
}
6 changes: 6 additions & 0 deletions test/fixtures/test-script/no-test-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test-script",
"scripts": {
"foo": "bar"
}
}
6 changes: 6 additions & 0 deletions test/fixtures/test-script/no-test-string/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test-script",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
}
6 changes: 6 additions & 0 deletions test/fixtures/test-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test-script",
"script": {
"test": "xo"
}
}
1 change: 1 addition & 0 deletions test/fixtures/travis/no-versions/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: node_js
3 changes: 3 additions & 0 deletions test/fixtures/travis/no-versions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "travis"
}
3 changes: 3 additions & 0 deletions test/fixtures/travis/php/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: php
php:
- '5.4'
3 changes: 3 additions & 0 deletions test/fixtures/travis/php/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "travis"
}
10 changes: 10 additions & 0 deletions test/fixtures/xo/ava/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "xo",
"scripts": {
"test": "ava"
},
"devDependencies": {
"ava": "*",
"xo": "*"
}
}

0 comments on commit c198c38

Please sign in to comment.