ソースを参照

test: add tests for background utils

Gerald 8 年 前
コミット
397d4a0022

+ 1 - 0
.eslintignore

@@ -1,3 +1,4 @@
 **/*.js
 !src/**
+!test/**
 src/public/**

+ 7 - 3
package.json

@@ -3,14 +3,16 @@
   "version": "2.6.0",
   "scripts": {
     "dev": "gulp watch",
+    "prebuild": "gulp clean",
     "build": "gulp build",
     "analyze": "webpack --profile --json --config scripts/webpack.conf.js | webpack-bundle-size-analyzer",
     "analyze:json": "webpack --profile --json --config scripts/webpack.conf.js > stats.json",
     "i18n": "gulp i18n",
-    "lint": "eslint --ext .js,.vue src",
+    "lint": "eslint --ext .js,.vue .",
     "update": "node scripts/update",
     "svgo": "svgo icons",
-    "test": "mocha"
+    "pretest": "webpack --config scripts/webpack.test.conf.js",
+    "test": "node dist/test"
   },
   "description": "Violentmonkey",
   "devDependencies": {
@@ -19,7 +21,6 @@
     "babel-loader": "^6.4.1",
     "babel-plugin-transform-runtime": "^6.23.0",
     "babel-preset-es2015": "^6.24.0",
-    "core-js": "^2.4.1",
     "css-loader": "^0.27.3",
     "cssnano": "^3.10.0",
     "del": "^2.2.0",
@@ -44,11 +45,13 @@
     "html-minifier": "^3.2.3",
     "html-webpack-plugin": "^2.28.0",
     "js-yaml": "^3.5.5",
+    "localStorage": "^1.0.3",
     "merge2": "^1.0.2",
     "mocha": "^3.1.2",
     "ncp": "^2.0.0",
     "precss": "^1.4.0",
     "svgo": "^0.7.2",
+    "tape": "^4.6.3",
     "through2": "^2.0.3",
     "vue-loader": "^11.3.3",
     "vue-style-loader": "^2.0.4",
@@ -68,6 +71,7 @@
   "license": "MIT",
   "dependencies": {
     "codemirror": "^5.25.0",
+    "core-js": "^2.4.1",
     "define-commonjs": "^1.1.2",
     "sync-promise-lite": "^0.2.3",
     "vue": "^2.2.5"

+ 62 - 0
scripts/webpack.base.conf.js

@@ -0,0 +1,62 @@
+const path = require('path');
+const webpack = require('webpack');
+const vueLoaderConfig = require('./vue-loader.conf');
+const utils = require('./utils');
+const DIST = 'dist';
+process.env.NODE_ENV = process.env.NODE_ENV || 'development';
+const IS_DEV = process.env.NODE_ENV === 'development';
+const definePlugin = new webpack.DefinePlugin({
+  'process.env': {
+    NODE_ENV: JSON.stringify(process.env.NODE_ENV),
+    DEBUG: IS_DEV ? 'true' : 'false', // whether to log message errors
+  },
+});
+
+function resolve(dir) {
+  return path.join(__dirname, '..', dir);
+}
+
+module.exports = {
+  output: {
+    path: resolve(DIST),
+    publicPath: '/',
+    filename: '[name].js',
+  },
+  resolve: {
+    extensions: ['.js', '.vue'],
+    alias: {
+      src: resolve('src'),
+    }
+  },
+  module: {
+    rules: [
+      // {
+      //   test: /\.(js|vue)$/,
+      //   loader: 'eslint-loader',
+      //   enforce: 'pre',
+      //   include: [resolve('src'), resolve('test')],
+      //   options: {
+      //     formatter: require('eslint-friendly-formatter')
+      //   }
+      // },
+      {
+        test: /\.vue$/,
+        loader: 'vue-loader',
+        options: vueLoaderConfig
+      },
+      {
+        test: /\.js$/,
+        loader: 'babel-loader',
+        include: [resolve('src'), resolve('test')]
+      },
+    ].concat(utils.styleLoaders({
+      sourceMap: false,
+      extract: !IS_DEV,
+    })),
+  },
+  // cheap-module-eval-source-map is faster for development
+  devtool: IS_DEV ? '#inline-source-map' : false,
+  plugins: [
+    definePlugin,
+  ],
+};

+ 6 - 59
scripts/webpack.conf.js

@@ -1,65 +1,9 @@
-const path = require('path');
 const webpack = require('webpack');
 const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
 const ExtractTextPlugin = require('extract-text-webpack-plugin');
 const HtmlWebpackPlugin = require('html-webpack-plugin');
-const utils = require('./utils');
-const vueLoaderConfig = require('./vue-loader.conf');
-process.env.NODE_ENV = process.env.NODE_ENV || 'development';
+const base = require('./webpack.base.conf');
 const IS_DEV = process.env.NODE_ENV === 'development';
-const DIST = 'dist';
-const definePlugin = new webpack.DefinePlugin({
-  'process.env': {
-    NODE_ENV: JSON.stringify(process.env.NODE_ENV),
-    DEBUG: IS_DEV ? 'true' : 'false', // whether to log message errors
-  },
-});
-
-function resolve(dir) {
-  return path.join(__dirname, '..', dir);
-}
-
-const base = {
-  output: {
-    path: resolve(DIST),
-    publicPath: '/',
-    filename: '[name].js',
-  },
-  resolve: {
-    extensions: ['.js', '.vue'],
-    alias: {
-      src: resolve('src'),
-    }
-  },
-  module: {
-    rules: [
-      // {
-      //   test: /\.(js|vue)$/,
-      //   loader: 'eslint-loader',
-      //   enforce: 'pre',
-      //   include: [resolve('src'), resolve('test')],
-      //   options: {
-      //     formatter: require('eslint-friendly-formatter')
-      //   }
-      // },
-      {
-        test: /\.vue$/,
-        loader: 'vue-loader',
-        options: vueLoaderConfig
-      },
-      {
-        test: /\.js$/,
-        loader: 'babel-loader',
-        include: [resolve('src'), resolve('test')]
-      },
-    ].concat(utils.styleLoaders({
-      sourceMap: false,
-      extract: !IS_DEV,
-    })),
-  },
-  // cheap-module-eval-source-map is faster for development
-  devtool: IS_DEV ? '#inline-source-map' : false,
-};
 
 const targets = module.exports = [];
 
@@ -70,7 +14,7 @@ targets.push(Object.assign({}, base, {
     'popup/app': 'src/popup/app.js',
   },
   plugins: [
-    definePlugin,
+    ... base.plugins,
     // split vendor js into its own file
     new webpack.optimize.CommonsChunkPlugin({
       name: 'vendor',
@@ -109,6 +53,9 @@ targets.push(Object.assign({}, base, {
       }),
     ],
   ],
+  externals: {
+    localStorage: 'localStorage',
+  },
 }));
 
 targets.push(Object.assign({}, base, {
@@ -117,7 +64,7 @@ targets.push(Object.assign({}, base, {
     browser: 'src/browser.js',
   },
   plugins: [
-    definePlugin,
+    ... base.plugins,
     ... IS_DEV ? [] : [
       new webpack.optimize.UglifyJsPlugin({
         compress: {

+ 9 - 0
scripts/webpack.test.conf.js

@@ -0,0 +1,9 @@
+const webpack = require('webpack');
+const base = require('./webpack.base.conf');
+
+module.exports = Object.assign({}, base, {
+  target: 'node',
+  entry: {
+    test: './test',
+  },
+});

+ 1 - 1
src/background/utils/options.js

@@ -1,5 +1,5 @@
 import { initHooks, debounce, normalizeKeys, object } from 'src/common';
-import storage from 'localStorage';
+import storage from 'localStorage';   // eslint-disable-line import/no-extraneous-dependencies
 
 const defaults = {
   isApplied: true,

+ 22 - 0
test/background/script.js

@@ -0,0 +1,22 @@
+import test from 'tape';
+import { isRemote, compareVersion } from 'src/background/utils/script';
+
+test('isRemote', t => {
+  t.notOk(isRemote());
+  t.notOk(isRemote('file:///tmp/file'));
+  t.notOk(isRemote('data:text/plain,hello,world'));
+  t.ok(isRemote('http://www.google.com'));
+  t.ok(isRemote('https://www.google.com'));
+  t.end();
+});
+
+test('compareVersion', t => {
+  t.equal(compareVersion('1.2.3', '1.2.3'), 0);
+  t.equal(compareVersion('1.2.3', '1.2.0'), 1);
+  t.equal(compareVersion('1.2.3', '1.2.4'), -1);
+  t.equal(compareVersion('1.2.0', '1.2'), 0);
+  t.equal(compareVersion('1.2.1', '1.2'), 1);
+  t.equal(compareVersion('1.1.9', '1.2'), -1);
+  t.equal(compareVersion('1.10', '1.9'), 1);
+  t.end();
+});

+ 105 - 0
test/background/tester.js

@@ -0,0 +1,105 @@
+import test from 'tape';
+import { testScript } from 'src/background/utils/tester';
+
+test('scheme', t => {
+  t.test('should match all', q => {
+    const script = {
+      custom: {},
+      meta: {
+        match: [
+          '*://*/*',
+        ],
+      },
+    };
+    q.ok(testScript('https://www.google.com/', script), 'should match `http | https`');
+    q.notOk(testScript('file:///Users/Gerald/file', script), 'should not match `file`');
+    q.end();
+  });
+
+  t.test('should match exact', q => {
+    const script = {
+      custom: {},
+      meta: {
+        match: [
+          'http://*/*',
+          'ftp://*/*',
+          'file:///*',
+        ],
+      },
+    };
+    q.ok(testScript('http://www.google.com/', script), 'should match `http`');
+    q.notOk(testScript('https://www.google.com/', script), 'should not match `https`');
+    q.ok(testScript('file:///Users/Gerald/file', script), 'should match `file`');
+    q.ok(testScript('ftp://example.com/file', script), 'should match `ftp`');
+    q.end();
+  });
+
+  t.end();
+});
+
+test('host', t => {
+  t.test('should match domain', q => {
+    const script = {
+      custom: {},
+      meta: {
+        match: [
+          '*://docs.google.com/',
+        ],
+      },
+    };
+    q.ok(testScript('https://docs.google.com/', script), 'should match exact domain name');
+    q.notOk(testScript('https://sub.docs.google.com/', script), 'should not match subdomains');
+    q.notOk(testScript('https://docs.google.com.cn/', script), 'should not match suffixed domains');
+    q.end();
+  });
+
+  t.test('should match subdomains', q => {
+    const script = {
+      custom: {},
+      meta: {
+        match: [
+          '*://*.google.com/',
+        ],
+      },
+    };
+    q.ok(testScript('https://www.google.com/', script), 'should match subdomains');
+    q.ok(testScript('https://a.b.google.com/', script), 'should match subdomains');
+    q.ok(testScript('https://google.com/', script), 'should match specified domain');
+    q.notOk(testScript('https://www.google.com.hk/', script), 'should not match suffixed domains');
+    q.end();
+  });
+
+  t.end();
+});
+
+test('path', t => {
+  t.test('should match any', q => {
+    const script = {
+      custom: {},
+      meta: {
+        match: [
+          'https://www.google.com/*',
+        ],
+      },
+    };
+    q.ok(testScript('https://www.google.com/', script), 'should match `/`');
+    q.ok(testScript('https://www.google.com/hello/world', script), 'should match any');
+    q.end();
+  });
+
+  t.test('should match exact', q => {
+    const script = {
+      custom: {},
+      meta: {
+        match: [
+          'https://www.google.com/a/b/c',
+        ],
+      },
+    };
+    q.ok(testScript('https://www.google.com/a/b/c', script), 'should match exact');
+    q.notOk(testScript('https://www.google.com/a/b/c/d', script), 'should match exact');
+    q.end();
+  });
+
+  t.end();
+});

+ 2 - 0
test/index.js

@@ -0,0 +1,2 @@
+import './background/tester';
+import './background/script';

+ 0 - 95
test/test_background.js

@@ -1,95 +0,0 @@
-const assert = require('assert');
-const tester = require('../src/background/utils/tester');
-
-describe('tester', () => {
-  describe('scheme', () => {
-    it('should match all', () => {
-      const script = {
-        custom: {},
-        meta: {
-          match: [
-            '*://*/*',
-          ],
-        }
-      };
-      assert.ok(tester.testScript('https://www.google.com/', script), 'should match `http | https`');
-      assert.ok(!tester.testScript('file:///Users/Gerald/file', script), 'should not match `file`');
-    });
-
-    it('should match exact', () => {
-      const script = {
-        custom: {},
-        meta: {
-          match: [
-            'http://*/*',
-            'ftp://*/*',
-            'file:///*',
-          ],
-        },
-      };
-      assert.ok(tester.testScript('http://www.google.com/', script), 'should match `http`');
-      assert.ok(!tester.testScript('https://www.google.com/', script), 'should not match `https`');
-      assert.ok(tester.testScript('file:///Users/Gerald/file', script), 'should match `file`');
-      assert.ok(tester.testScript('ftp://example.com/file', script), 'should match `ftp`');
-    });
-  });
-
-  describe('host', () => {
-    it('should match domain', () => {
-      const script = {
-        custom: {},
-        meta: {
-          match: [
-            '*://docs.google.com/',
-          ],
-        },
-      };
-      assert.ok(tester.testScript('https://docs.google.com/', script), 'should match exact domain name');
-      assert.ok(!tester.testScript('https://sub.docs.google.com/', script), 'should not match subdomains');
-      assert.ok(!tester.testScript('https://docs.google.com.cn/', script), 'should not match suffixed domains');
-    });
-
-    it('should match subdomains', () => {
-      const script = {
-        custom: {},
-        meta: {
-          match: [
-            '*://*.google.com/',
-          ],
-        },
-      };
-      assert.ok(tester.testScript('https://www.google.com/', script), 'should match subdomains');
-      assert.ok(tester.testScript('https://a.b.google.com/', script), 'should match subdomains');
-      assert.ok(tester.testScript('https://google.com/', script), 'should match specified domain');
-      assert.ok(!tester.testScript('https://www.google.com.hk/', script), 'should not match suffixed domains');
-    });
-  });
-
-  describe('path', () => {
-    it('should match any', () => {
-      const script = {
-        custom: {},
-        meta: {
-          match: [
-            'https://www.google.com/*',
-          ],
-        },
-      };
-      assert.ok(tester.testScript('https://www.google.com/', script), 'should match `/`');
-      assert.ok(tester.testScript('https://www.google.com/hello/world', script), 'should match any');
-    });
-
-    it('should match exact', () => {
-      const script = {
-        custom: {},
-        meta: {
-          match: [
-            'https://www.google.com/a/b/c',
-          ],
-        },
-      };
-      assert.ok(tester.testScript('https://www.google.com/a/b/c', script), 'should match exact');
-      assert.ok(!tester.testScript('https://www.google.com/a/b/c/d', script), 'should match exact');
-    });
-  });
-});