diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..3b1360e --- /dev/null +++ b/.eslintrc @@ -0,0 +1,3 @@ +{ + "extends": "think" +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 4bbaabc..21f9e02 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,7 @@ LABEL maintainer="lizheming " \ WORKDIR /wechat COPY package.json /wechat/package.json -RUN npm install --registry=https://registry.npm.taobao.org +RUN npm install --production --registry=https://registry.npm.taobao.org COPY index.js /wechat/index.js ENTRYPOINT [ "node", "/wechat/index.js" ] \ No newline at end of file diff --git a/index.js b/index.js index 2edf153..c6dddd7 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +const process = require('process'); const render = require('drone-render'); const request = require('request-promise-native'); @@ -16,4 +17,4 @@ request({ text: PLUGIN_TITLE, desp: render(PLUGIN_MESSAGE) } -}); \ No newline at end of file +}); diff --git a/package.json b/package.json index 65e90a5..daa4f3e 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "description": "drone wechat notification plugin", "main": "index.js", "scripts": { - "test": "\"\"" + "test": "npm run eslint && npm run test-cov", + "test-cov": "nyc ava test.js && nyc report --reporter=html", + "eslint": "eslint index.js" }, "repository": { "type": "git", @@ -26,5 +28,12 @@ "drone-render": "^1.0.0", "request": "^2.83.0", "request-promise-native": "^1.0.5" + }, + "devDependencies": { + "ava": "^0.24.0", + "eslint": "^4.14.0", + "eslint-config-think": "^1.0.2", + "mock-require": "^2.0.2", + "nyc": "^11.4.1" } } diff --git a/test.js b/test.js new file mode 100644 index 0000000..476f6bb --- /dev/null +++ b/test.js @@ -0,0 +1,51 @@ +const {test} = require('ava'); +const mock = require('mock-require'); + +const SCKEY = 'testsckey'; +const SCKEY2 = 'testsckey2'; +const TITLE = 'test for title'; +const MESSAGE = 'test for message'; + +test('send wechat', async t => { + t.plan(2); + + mock('process', { + env: { + PLUGIN_SCKEY: SCKEY, + PLUGIN_TITLE: TITLE, + PLUGIN_MESSAGE: MESSAGE + } + }); + mock('drone-render', text => { + t.is(text, MESSAGE); + return text + '1'; + }); + mock('request-promise-native', obj => { + t.deepEqual(obj, { + url: `https://sc.ftqq.com/${SCKEY}.send`, + qs: { + text: TITLE, + desp: MESSAGE + '1' + } + }); + }); + + mock.reRequire('./index.js'); + mock.stopAll(); +}); + +test('send wechat with SERVER_CHAN_KEY', async t => { + mock('process', { + env: { + SERVER_CHAN_KEY: SCKEY2, + PLUGIN_TITLE: TITLE, + PLUGIN_MESSAGE: MESSAGE + } + }); + mock('request-promise-native', obj => { + t.is(obj.url, `https://sc.ftqq.com/${SCKEY2}.send`); + }); + + mock.reRequire('./index.js'); + mock.stopAll(); +});