Repository: infinitered/solidarity
Pull request #196 (2018-03-31 04:06:32)

improve plugin flow and tests
Found a mistake in plugin flow.  Fixed it and added better testing.

codecov[bot]: # [Codecov](https://codecov.io/gh/infinitered/solidarity/pull/196?src=pr&el=h1) Report
> Merging [#196](https://codecov.io/gh/infinitered/solidarity/pull/196?src=pr&el=desc) into [master](https://codecov.io/gh/infinitered/solidarity/commit/d5ed9d42ece342a124648ffead446850e26ed925?src=pr&el=desc) will **increase** coverage by `1.19%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/infinitered/solidarity/pull/196/graphs/tree.svg?token=eTjcyCooFB&height=150&width=650&src=pr)](https://codecov.io/gh/infinitered/solidarity/pull/196?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #196      +/-   ##
==========================================
+ Coverage   80.88%   82.07%   +1.19%     
==========================================
  Files          33       33              
  Lines         633      636       +3     
  Branches      111      112       +1     
==========================================
+ Hits          512      522      +10     
+ Misses         83       78       -5     
+ Partials       38       36       -2
```


| [Impacted Files](https://codecov.io/gh/infinitered/solidarity/pull/196?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/extensions/functions/createPlugin.ts](https://codecov.io/gh/infinitered/solidarity/pull/196/diff?src=pr&el=tree#diff-c3JjL2V4dGVuc2lvbnMvZnVuY3Rpb25zL2NyZWF0ZVBsdWdpbi50cw==) | `96.15% <100%> (+30.93%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/infinitered/solidarity/pull/196?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/infinitered/solidarity/pull/196?src=pr&el=footer). Last update [d5ed9d4...6ca2595](https://codecov.io/gh/infinitered/solidarity/pull/196?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/extensions/functions/createPlugin.ts b/src/extensions/functions/createPlugin.ts
index 580f527..a5a9be5 100644
--- a/src/extensions/functions/createPlugin.ts
+++ b/src/extensions/functions/createPlugin.ts
@@ -9,7 +9,7 @@ module.exports = async context => {
     message: 'Plugin name? (we will add the namespacing for you)',
   })
   if (!answerPluginName.plugin) throw Error('A plugin requires a name')
-  const pluginName = `solidarity-${answerPluginName.plugin.replace('solidarity-', '')}`
+  const pluginName = `solidarity-${answerPluginName.plugin.replace('solidarity-', '').toLowerCase()}`
 
   const description = await prompt.ask({
     type: 'input',
@@ -29,7 +29,10 @@ module.exports = async context => {
     choices: ruleChoices,
   })
 
-  if (answer.ruleChoice === ruleChoices[1]) {
+  const noRuleTemplate = answer.ruleChoice === ruleChoices[0]
+  if (noRuleTemplate) {
+    files.push(['simple-plugin.js.ejs', `extensions/${pluginName}.js`])
+  } else if (answer.ruleChoice === ruleChoices[1]) {
     files.push(['rules-template.json.ejs', `templates/${pluginName}-template.json`])
     files.push(['simple-plugin.js.ejs', `extensions/${pluginName}.js`])
   } else if (answer.ruleChoice === ruleChoices[2]) {
@@ -46,7 +49,7 @@ module.exports = async context => {
     template.generate({
       template: fileSet[0],
       target: `${pluginName}/${fileSet[1]}`,
-      props: { pluginName, customRules, description: description.pluginDesc },
+      props: { pluginName, customRules, description: description.pluginDesc, noRuleTemplate },
     })
   })
 
diff --git a/src/templates/simple-plugin.js.ejs b/src/templates/simple-plugin.js.ejs
index dff1bd6..9f96ba9 100644
--- a/src/templates/simple-plugin.js.ejs
+++ b/src/templates/simple-plugin.js.ejs
@@ -2,8 +2,8 @@ module.exports = (context) => {
   // Register this plugin
   context.addPlugin({
     name: '<%= props.pluginName %>',
-    description: '<%= props.description %>',
-    snapshot: '<%= props.pluginName %>-template.json'<% if(props.customRules){ %>,
+    description: '<%= props.description %>'<% if(!props.noRuleTemplate){ %>,
+    snapshot: '<%= props.pluginName %>-template.json'<% } %><% if(props.customRules){ %>,
     rules: {
       ruleName: {
         check: async (rule, context) => {


Test patch:
diff --git a/__tests__/command_helpers/__snapshots__/createPlugin.ts.snap b/__tests__/command_helpers/__snapshots__/createPlugin.ts.snap
index 34dd200..12276d3 100644
--- a/__tests__/command_helpers/__snapshots__/createPlugin.ts.snap
+++ b/__tests__/command_helpers/__snapshots__/createPlugin.ts.snap
@@ -2,6 +2,81 @@
 
 exports[`check result shape 1`] = `[Function]`;
 
+exports[`checking plugin paths Choice 1 - 'I do not want a generated rule file' 1`] = `
+Array [
+  Array [
+    ".gitignore.ejs",
+    ".gitignore",
+  ],
+  Array [
+    "README.md.ejs",
+    "README.md",
+  ],
+  Array [
+    "package.json.ejs",
+    "package.json",
+  ],
+  Array [
+    "simple-plugin.js.ejs",
+    "extensions/solidarity-nachos.js",
+  ],
+]
+`;
+
+exports[`checking plugin paths Choice 2 - 'Just a simple rule template' 1`] = `
+Array [
+  Array [
+    ".gitignore.ejs",
+    ".gitignore",
+  ],
+  Array [
+    "README.md.ejs",
+    "README.md",
+  ],
+  Array [
+    "package.json.ejs",
+    "package.json",
+  ],
+  Array [
+    "rules-template.json.ejs",
+    "templates/solidarity-nachos-template.json",
+  ],
+  Array [
+    "simple-plugin.js.ejs",
+    "extensions/solidarity-nachos.js",
+  ],
+]
+`;
+
+exports[`checking plugin paths Choice 3 - 'Template + optional rules' 1`] = `
+Array [
+  Array [
+    ".gitignore.ejs",
+    ".gitignore",
+  ],
+  Array [
+    "README.md.ejs",
+    "README.md",
+  ],
+  Array [
+    "package.json.ejs",
+    "package.json",
+  ],
+  Array [
+    "rules-template.json.ejs",
+    "templates/solidarity-nachos-template.json",
+  ],
+  Array [
+    "helpful-plugin.js.ejs",
+    "extensions/solidarity-nachos.js",
+  ],
+  Array [
+    "addOptionalRules.js.ejs",
+    "extensions/helpers/addOptionalRules.js",
+  ],
+]
+`;
+
 exports[`investigate createPlugin 1`] = `
 Array [
   Array [
diff --git a/__tests__/command_helpers/createPlugin.ts b/__tests__/command_helpers/createPlugin.ts
index ccdf52e..6d5c2de 100644
--- a/__tests__/command_helpers/createPlugin.ts
+++ b/__tests__/command_helpers/createPlugin.ts
@@ -13,3 +13,43 @@ test('investigate createPlugin', async () => {
   expect(context.prompt.confirm).toBeCalled()
   expect(context.print.success).toBeCalled()
 })
+
+describe('checking plugin paths', () => {
+
+  beforeEach(() => {
+    const mockedPrompt = jest
+      .fn()
+      .mockImplementationOnce(() => Promise.resolve({ plugin: 'Nachos' }))
+      .mockImplementationOnce(() => Promise.resolve({ pluginDesc: 'das nachos plugin' }))
+
+    context.prompt.ask = mockedPrompt
+  })
+
+  test(`Choice 1 - 'I do not want a generated rule file'`, async () => {
+    context.prompt.ask.mockImplementationOnce(() => Promise.resolve({
+      ruleChoice: 'I do not want a generated rule file'
+    }))
+
+    const result = await createPlugin(context)
+    expect(result).toMatchSnapshot()
+  })
+
+  test(`Choice 2 - 'Just a simple rule template'`, async () => {
+    context.prompt.ask.mockImplementationOnce(() => Promise.resolve({
+      ruleChoice: 'Just a simple rule template'
+    }))
+
+    const result = await createPlugin(context)
+    expect(result).toMatchSnapshot()
+  })
+
+  test(`Choice 3 - 'Template + optional rules'`, async () => {
+    context.prompt.ask.mockImplementationOnce(() => Promise.resolve({
+      ruleChoice: 'Template + optional rules'
+    }))
+
+    const result = await createPlugin(context)
+    expect(result).toMatchSnapshot()
+  })
+
+})