Skip to content

Commit

Permalink
Add dry-run option (#1141)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored and SBoudrias committed Dec 24, 2019
1 parent 99f0360 commit 4c2a675
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ class Generator extends EventEmitter {
this.conflicter = new Conflicter(this.env.adapter, this.options.force, {
bail: this.options.bail,
ignoreWhitespace: this.options.whitespace,
skipRegenerate: this.options.skipRegenerate
skipRegenerate: this.options.skipRegenerate,
dryRun: this.options.dryRun
});

// Mirror the adapter log method on the generator.
Expand Down
17 changes: 17 additions & 0 deletions lib/util/conflicter.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Conflicter {
this.bail = options.bail;
this.ignoreWhitespace = options.ignoreWhitespace;
this.skipRegenerate = options.skipRegenerate;
this.dryRun = options.dryRun;
}

this.force = force === true;
Expand All @@ -56,6 +57,11 @@ class Conflicter {
this.ignoreWhitespace = true;
this.skipRegenerate = true;
}

if (this.dryRun) {
// Ignore whitespace changes with "ignoreWhitespace === true" option
this.skipRegenerate = true;
}
}

/**
Expand Down Expand Up @@ -210,6 +216,11 @@ class Conflicter {
throw new ConflictError();
}

if (this.dryRun) {
cb('skip');
return;
}

cb('create');
return;
}
Expand All @@ -228,6 +239,12 @@ class Conflicter {
throw new ConflictError();
}

if (this.dryRun) {
this._printDiff(file);
cb('skip');
return;
}

this._ask(file, cb);
} else {
this.adapter.log.identical(rfilepath);
Expand Down
69 changes: 69 additions & 0 deletions test/conflicter.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,75 @@ describe('Conflicter', () => {
done();
});

it('skip file changes with dryRun', function(done) {
const conflicter = new Conflicter(new TestAdapter(), false, {
dryRun: true
});
conflicter.collision(
{
path: path.join(__dirname, 'fixtures/file-conflict.txt'),
contents: `initial
content
`
},
status => {
assert.equal(status, 'skip');
done();
}
);
});

it('skip new file with dryRun', function(done) {
const conflicter = new Conflicter(new TestAdapter(), false, {
dryRun: true
});
conflicter.collision(
{
path: 'file-who-does-not-exist2.js',
contents: ''
},
status => {
assert.equal(status, 'skip');
done();
}
);
});

it('skip deleted file with dryRun', function(done) {
const conflicter = new Conflicter(new TestAdapter(), false, {
dryRun: true
});
conflicter.collision(
{
path: path.join(__dirname, 'fixtures/foo.js'),
contents: null
},
status => {
assert.equal(status, 'skip');
done();
}
);
});

it('skip whitespace changes with dryRun', function(done) {
const conflicter = new Conflicter(new TestAdapter(), false, {
dryRun: true,
ignoreWhitespace: true
});
conflicter.collision(
{
path: path.join(__dirname, 'fixtures/file-conflict.txt'),
contents: `initial
content
`
},
status => {
assert.equal(status, 'skip');
done();
}
);
});

it('does not give a conflict with ignoreWhitespace', function(done) {
const conflicter = new Conflicter(new TestAdapter(), false, {
ignoreWhitespace: true
Expand Down

0 comments on commit 4c2a675

Please sign in to comment.