Skip to content

Commit

Permalink
Update eslint settings and add prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Jan 6, 2022
1 parent 17572ed commit 8e8ea6c
Show file tree
Hide file tree
Showing 37 changed files with 483 additions and 276 deletions.
22 changes: 15 additions & 7 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
{
"extends": "xo-space",
"extends": ["xo", "prettier"],
"env": {
"jest": true
"mocha": true,
"jest": true,
"node": true
},
"rules": {
"default-param-last": "off",
"padding-line-between-statements": "off",
"no-eq-null": "off",
"eqeqeq": [
2,
"allow-null"
"error",
"always",
{
"null": "ignore"
}
],
"no-eq-null": 0,
"max-params": "off"
}
"prettier/prettier": "error"
},
"plugins": ["prettier"]
}
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"endOfLine": "auto",
"singleQuote": true,
"printWidth": 90
}
6 changes: 1 addition & 5 deletions __tests__/append-tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ describe('#appendTpl()', () => {
const orginalContent = fs.read(filepath).toString();
const contentPath = path.join(__dirname, 'fixtures/file-tpl-custom-delimiter.txt');
const contents = fs.read(contentPath);
fs.appendTpl(filepath, contents, {
name: 'bar',
}, {
delimiter: '?',
});
fs.appendTpl(filepath, contents, { name: 'bar' }, { delimiter: '?' });
expect(fs.read(filepath)).toBe(orginalContent + 'bar' + os.EOL);
});

Expand Down
6 changes: 3 additions & 3 deletions __tests__/append.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('#write()', () => {
});

it('appends new content to new file', () => {
fs.append('append.txt', 'b', {create: true});
fs.append('append.txt', 'b', { create: true });

expect(fs.read('append.txt')).toBe('b');
});
Expand All @@ -28,14 +28,14 @@ describe('#write()', () => {

it('allows specifying custom separator', () => {
fs.write('append.txt', 'a');
fs.append('append.txt', 'b', {separator: ', '});
fs.append('append.txt', 'b', { separator: ', ' });

expect(fs.read('append.txt')).toBe('a, b');
});

it('allows disabling end trim', () => {
fs.write('append.txt', 'a\n\n');
fs.append('append.txt', 'b', {trimEnd: false});
fs.append('append.txt', 'b', { trimEnd: false });

expect(fs.read('append.txt')).toBe('a\n\n' + os.EOL + 'b');
});
Expand Down
10 changes: 7 additions & 3 deletions __tests__/commit-file-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('#commit()', () => {
});

afterEach(() => {
rmSync(outputRoot, {recursive: true});
rmSync(outputRoot, { recursive: true });
});

it('triggers callback when done', async () => {
Expand All @@ -38,11 +38,15 @@ describe('#commit()', () => {
});

it('adds non existing file to store', async () => {
await fs.commitFileAsync({path: filenameNew, contents: Buffer.from('bar'), state: 'modified'});
await fs.commitFileAsync({
path: filenameNew,
contents: Buffer.from('bar'),
state: 'modified',
});
expect(store.add.callCount).toEqual(2);
});

it('doesn\'t readd same file to store', async () => {
it("doesn't readd same file to store", async () => {
await fs.commitFileAsync(store.get(filename));
expect(store.add.callCount).toEqual(1);
});
Expand Down
95 changes: 53 additions & 42 deletions __tests__/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const path = require('path');
const memFs = require('mem-fs');
const sinon = require('sinon');
const editor = require('..');
const {createTransform} = require('../lib/util');
const { createTransform } = require('../lib/util');

const rmSync = filesystem.rmSync || filesystem.rmdirSync;

Expand All @@ -21,7 +21,7 @@ describe('#commit()', () => {
beforeEach(() => {
store = memFs.create();
fs = editor.create(store);
filesystem.mkdirSync(fixtureDir, {recursive: true, force: true});
filesystem.mkdirSync(fixtureDir, { recursive: true, force: true });

// Create a 100 files to exercise the stream high water mark
let i = NUMBER_FILES;
Expand All @@ -33,37 +33,37 @@ describe('#commit()', () => {
});

afterEach(() => {
rmSync(fixtureDir, {recursive: true, force: true});
rmSync(output, {recursive: true, force: true});
rmSync(fixtureDir, { recursive: true, force: true });
rmSync(output, { recursive: true, force: true });
});

it('triggers callback when done', done => {
it('triggers callback when done', (done) => {
fs.commit(done);
});

it('should match snapshot', done => {
fs.commit(error => {
it('should match snapshot', (done) => {
fs.commit((error) => {
expect(fs.dump(output)).toMatchSnapshot();
done(error);
});
});

it('call filters and trigger callback on error', done => {
it('call filters and trigger callback on error', (done) => {
let called = 0;

const filter = createTransform((file, enc, cb) => {
called++;
cb(new Error(`error ${called}`));
});

fs.commit([filter], error => {
fs.commit([filter], (error) => {
expect(called).toBe(1);
expect(error.message).toBe('error 1');
done();
});
});

it('call filters and update memory model', done => {
it('call filters and update memory model', (done) => {
let called = 0;

const filter = createTransform(function (file, enc, cb) {
Expand All @@ -80,7 +80,7 @@ describe('#commit()', () => {
});
});

it('call filters, update memory model and commit selected files', done => {
it('call filters, update memory model and commit selected files', (done) => {
let called = 0;

const filter = createTransform(function (file, enc, cb) {
Expand Down Expand Up @@ -108,8 +108,8 @@ describe('#commit()', () => {
});
});

it('write file to disk', done => {
fs.commit(error => {
it('write file to disk', (done) => {
fs.commit((error) => {
expect(filesystem.existsSync(path.join(output, 'file-1.txt'))).toBeTruthy();
expect(filesystem.existsSync(path.join(output, 'file-1.txt'))).toBeTruthy();
expect(filesystem.existsSync(path.join(output, 'file-50.txt'))).toBeTruthy();
Expand All @@ -118,9 +118,9 @@ describe('#commit()', () => {
});
}, 10000);

it('handle error when write fails', done => {
it('handle error when write fails', (done) => {
filesystem.writeFileSync(output, 'foo');
fs.commit(async error => {
fs.commit(async (error) => {
filesystem.unlinkSync(output);
if (error) {
done();
Expand All @@ -131,9 +131,9 @@ describe('#commit()', () => {
});
});

it('delete file from disk', done => {
it('delete file from disk', (done) => {
const file = path.join(output, 'delete.txt');
filesystem.mkdirSync(output, {recursive: true, force: true});
filesystem.mkdirSync(output, { recursive: true, force: true });
filesystem.writeFileSync(file, 'to delete');

fs.delete(file);
Expand All @@ -144,9 +144,12 @@ describe('#commit()', () => {
});
});

it('delete directories from disk', done => {
it('delete directories from disk', (done) => {
const file = path.join(output, 'nested/delete.txt');
filesystem.mkdirSync(path.join(output, 'nested'), {recursive: true, force: true});
filesystem.mkdirSync(path.join(output, 'nested'), {
recursive: true,
force: true,
});
filesystem.writeFileSync(file, 'to delete');

fs.delete(path.join(output, 'nested'));
Expand All @@ -156,33 +159,36 @@ describe('#commit()', () => {
});
});

it('reset file status after commiting', done => {
it('reset file status after commiting', (done) => {
fs.commit(() => {
expect(fs.store.get(path.join(output, '/file-a.txt')).state).toBeUndefined();
done();
});
});

it('does not commit files who are deleted before being commited', done => {
it('does not commit files who are deleted before being commited', (done) => {
fs.write('to-delete', 'foo');
fs.delete('to-delete');
fs.copy(path.join(__dirname, 'fixtures/file-a.txt'), 'copy-to-delete');
fs.delete('copy-to-delete');
fs.store.get('to-delete');

fs.commitFileAsync = sinon.stub().returns(Promise.resolve());
fs.commit([
createTransform(function (file, enc, cb) {
expect(file.path).not.toEqual(path.resolve('to-delete'));
expect(file.path).not.toEqual(path.resolve('copy-to-delete'));

this.push(file);
cb();
}),
], () => {
expect(fs.commitFileAsync.callCount).toBe(NUMBER_FILES);
done();
});
fs.commit(
[
createTransform(function (file, enc, cb) {
expect(file.path).not.toEqual(path.resolve('to-delete'));
expect(file.path).not.toEqual(path.resolve('copy-to-delete'));

this.push(file);
cb();
}),
],
() => {
expect(fs.commitFileAsync.callCount).toBe(NUMBER_FILES);
done();
}
);
});
});

Expand All @@ -200,11 +206,11 @@ describe('#copy() and #commit()', () => {
});

afterEach(() => {
rmSync(output, {recursive: true, force: true});
rmSync(output, { recursive: true, force: true });
});

it('should match snapshot', done => {
fs.commit(error => {
it('should match snapshot', (done) => {
fs.commit((error) => {
expect(fs.dump(output)).toMatchSnapshot();
done(error);
});
Expand All @@ -221,19 +227,24 @@ describe('#copyTpl() and #commit()', () => {
store = memFs.create();
fs = editor.create(store);

const a = {name: 'foo'};
const b = {a};
const a = { name: 'foo' };
const b = { a };
a.b = b;

fs.copyTpl(path.join(__dirname, 'fixtures', '**'), output, {name: 'bar'}, {context: {a}});
fs.copyTpl(
path.join(__dirname, 'fixtures', '**'),
output,
{ name: 'bar' },
{ context: { a } }
);
});

afterEach(() => {
rmSync(output, {recursive: true, force: true});
rmSync(output, { recursive: true, force: true });
});

it('should match snapshot', done => {
fs.commit(error => {
it('should match snapshot', (done) => {
fs.commit((error) => {
expect(fs.dump(output)).toMatchSnapshot();
done(error);
});
Expand Down
24 changes: 13 additions & 11 deletions __tests__/copy-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ describe('#copyAsync()', () => {
const filepath = path.join(__dirname, 'fixtures/file-a.txt');
const initialContents = fs.read(filepath);
const newPath = '/new/path/file.txt';
await fs.copyAsync(filepath, newPath, {append: true});
await fs.copyAsync(filepath, newPath, { append: true });

expect(fs.write.callCount).toBe(1);
expect(fs.append.callCount).toBe(0);
expect(fs.read(newPath)).toBe(initialContents);
expect(fs.store.get(newPath).state).toBe('modified');

await fs.copyAsync(filepath, newPath, {append: true});
await fs.copyAsync(filepath, newPath, { append: true });

expect(fs.write.callCount).toBe(2);
expect(fs.append.callCount).toBe(1);
Expand All @@ -57,7 +57,9 @@ describe('#copyAsync()', () => {
store.existsInMemory = undefined;
const filepath = path.join(__dirname, 'fixtures/file-a.txt');
const newPath = '/new/path/file.txt';
expect(fs.copyAsync(filepath, newPath, {append: true, processFile: () => ''})).rejects.toEqual(new Error('Current mem-fs is not compatible with append'));
expect(
fs.copyAsync(filepath, newPath, { append: true, processFile: () => '' })
).rejects.toEqual(new Error('Current mem-fs is not compatible with append'));
});
});

Expand Down Expand Up @@ -119,7 +121,9 @@ describe('#copyAsync()', () => {
const processFile = sinon.stub().callsFake(function (from) {
return this.store.get(from).contents;
});
await fs.copyAsync(path.join(__dirname, '/fixtures/**'), outputDir, {processFile});
await fs.copyAsync(path.join(__dirname, '/fixtures/**'), outputDir, {
processFile,
});
sinon.assert.callCount(processFile, 13); // 10 total files under 'fixtures', not counting folders
expect(fs.read(path.join(outputDir, 'file-a.txt'))).toBe('foo' + os.EOL);
expect(fs.read(path.join(outputDir, '/nested/file.txt'))).toBe('nested' + os.EOL);
Expand All @@ -138,26 +142,24 @@ describe('#copyAsync()', () => {
path.join(__dirname, '/fixtures/file-a.txt'),
outputFile,
{},
{category: 'foo'},
{ category: 'foo' }
);
expect(
fs.read(path.join(__dirname, 'test/foo/file-a.txt')),
).toBe('foo' + os.EOL);
expect(fs.read(path.join(__dirname, 'test/foo/file-a.txt'))).toBe('foo' + os.EOL);
});

it('requires destination directory when globbing', async () => {
expect(
fs.copyAsync(
path.join(__dirname, '/fixtures/**'),
path.join(__dirname, '/fixtures/file-a.txt'),
),
path.join(__dirname, '/fixtures/file-a.txt')
)
).rejects.toThrow();
});

it('preserve permissions', async () => {
const filename = path.join(os.tmpdir(), 'perm.txt');
const copyname = path.join(os.tmpdir(), 'copy-perm.txt');
filesystem.writeFileSync(filename, 'foo', {mode: parseInt(733, 8)});
filesystem.writeFileSync(filename, 'foo', { mode: parseInt(733, 8) });

await fs.copyAsync(filename, copyname);

Expand Down
Loading

0 comments on commit 8e8ea6c

Please sign in to comment.