Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.

Commit

Permalink
fixes issue with append action when unique is used with a string pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
amwmedia committed Oct 18, 2019
1 parent f518e91 commit 8b8bb3c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-plop",
"version": "0.20.0",
"version": "0.21.0",
"description": "programmatic plopping for fun and profit",
"main": "lib/index.js",
"types": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/actions/append.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const doAppend = function*(data, cfg, plop, fileData) {
// if the appended string should be unique (default),
// remove any occurence of it (but only if pattern would match)

if (cfg.unique !== false && new RegExp(cfg.pattern).test(fileData)) {
if (cfg.unique !== false) {
// only remove after "pattern", so that we remove not too much accidentally
const parts = fileData.split(cfg.pattern);
const lastPart = parts[parts.length - 1];
Expand Down
2 changes: 0 additions & 2 deletions tests/append-mock/plop-templates/list-item.txt

This file was deleted.

2 changes: 2 additions & 0 deletions tests/append-mock/plop-templates/list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ Don't remove me: Plop

-- APPEND ITEMS HERE --

/* APPEND OTHER ITEMS HERE */

+++++++++++++++++++++++++++++++++++++++
9 changes: 8 additions & 1 deletion tests/append-mock/plopfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ module.exports = function (plop) {
type: 'append',
path: 'src/{{listName}}.txt',
pattern: /-- APPEND ITEMS HERE --/gi,
templateFile: 'plop-templates/list-item.txt',
template: '😻 name: {{name}}1',
unique: !allowDuplicates
},
{
type: 'append',
path: 'src/{{listName}}.txt',
pattern: '/* APPEND OTHER ITEMS HERE */',
template: '🔥 name: {{name}}2',
unique: !allowDuplicates
}
]
Expand Down
27 changes: 19 additions & 8 deletions tests/append.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ test('Check if entry will be appended', co.wrap(function* (t) {
const filePath = path.resolve(testSrcPath, 'list1.txt');
const content = fs.readFileSync(filePath).toString();

t.is((content.match(/Marco/g) || []).length, 1);
t.is((content.match(/Polo/g) || []).length, 1);
t.is((content.match(/Marco1/g) || []).length, 1);
t.is((content.match(/Polo1/g) || []).length, 1);
t.is((content.match(/Marco2/g) || []).length, 1);
t.is((content.match(/Polo2/g) || []).length, 1);
}));

test('Check if duplicates get filtered', co.wrap(function* (t) {
Expand All @@ -34,8 +36,10 @@ test('Check if duplicates get filtered', co.wrap(function* (t) {
const filePath = path.resolve(testSrcPath, 'list2.txt');
const content = fs.readFileSync(filePath).toString();

t.is((content.match(/Marco/g) || []).length, 1);
t.is((content.match(/Polo/g) || []).length, 1);
t.is((content.match(/Marco1/g) || []).length, 1);
t.is((content.match(/Polo1/g) || []).length, 1);
t.is((content.match(/Marco2/g) || []).length, 1);
t.is((content.match(/Polo2/g) || []).length, 1);
}));

test('Check if duplicates are kept, if allowed', co.wrap(function* (t) {
Expand All @@ -46,8 +50,10 @@ test('Check if duplicates are kept, if allowed', co.wrap(function* (t) {
const filePath = path.resolve(testSrcPath, 'list3.txt');
const content = fs.readFileSync(filePath).toString();

t.is((content.match(/Marco/g) || []).length, 2);
t.is((content.match(/Polo/g) || []).length, 1);
t.is((content.match(/Marco1/g) || []).length, 2);
t.is((content.match(/Polo1/g) || []).length, 1);
t.is((content.match(/Marco2/g) || []).length, 2);
t.is((content.match(/Polo2/g) || []).length, 1);
}));

test('Check if duplicates are only removed below the pattern', co.wrap(function* (t) {
Expand All @@ -59,6 +65,11 @@ test('Check if duplicates are only removed below the pattern', co.wrap(function*
const filePath = path.resolve(testSrcPath, 'list4.txt');
const content = fs.readFileSync(filePath).toString();

t.is((content.match(/Plop/g) || []).length, 2);
t.is((content.match(/Polo/g) || []).length, 1);
t.is((content.match(/Plop1/g) || []).length, 1);
t.is((content.match(/Polo1/g) || []).length, 1);
t.is((content.match(/Plop2/g) || []).length, 1);
t.is((content.match(/Polo2/g) || []).length, 1);

// there's a plop at the top
t.is((content.match(/Plop/g) || []).length, 3);
}));

0 comments on commit 8b8bb3c

Please sign in to comment.