Browse Source

add tests for npm install

Vanessa Yuen 7 years ago
parent
commit
40555371ba
3 changed files with 106 additions and 11 deletions
  1. 5 2
      npm/package.json
  2. 86 9
      npm/test/index.js
  3. 15 0
      npm/test/test.js

+ 5 - 2
npm/package.json

@@ -2,8 +2,8 @@
   "scripts": {
     "cache-clean": "rm -rf ~/.electron && rm -rf dist",
     "postinstall": "node install.js",
-    "pretest": "npm run cache-clean && npm run postinstall",
-    "test": "tape test/*.js && standard"
+    "pretest": "npm run cache-clean",
+    "test": "tape test/index.js && standard"
   },
   "bin": {
     "electron": "cli.js"
@@ -16,8 +16,11 @@
     "extract-zip": "^1.0.3"
   },
   "devDependencies": {
+    "adm-zip": "^0.4.7",
     "home-path": "^0.1.1",
     "path-exists": "^2.0.0",
+    "proxyquire": "^1.8.0",
+    "sinon": "^2.3.8",
     "standard": "^5.4.1",
     "tape": "^3.0.1"
   },

+ 86 - 9
npm/test/index.js

@@ -1,15 +1,92 @@
-var tape = require('tape')
-var electron = require('../')
-var path = require('path')
-var pathExists = require('path-exists')
-var getHomePath = require('home-path')()
+const tape = require('tape')
+const proxyquire = require('proxyquire')
+const path = require('path')
+const sinon = require('sinon')
+const admZip = require('adm-zip')
+const temp = require('temp')
+// var pathExists = require('path-exists')
+// var getHomePath = require('home-path')()
 
-tape('has local binary', function (t) {
-  t.ok(pathExists.sync(electron), 'electron was downloaded')
+let sandbox
+const mockEnv = {
+  electron_config_cache: 'cache',
+  npm_config_platform: 'linux',
+  npm_config_arch: 'win32',
+  npm_config_strict_ssl: 'true',
+  force_no_cache: 'false',
+  npm_config_loglevel: 'silly'
+}
+let tempDir
+temp.track()
+
+tape('set up', (t) => {
+  sandbox = sinon.sandbox.create()
+  tempDir = temp.mkdirSync('electron-install')
+  console.log(tempDir)
+  t.end()
+})
+
+tape('download electron', (t) => {
+  const downloadSpy = sinon.spy()
+
+  sandbox.stub(process, 'env').value(mockEnv)
+  proxyquire(path.join(__dirname, '..', 'install.js'), {
+    'electron-download': downloadSpy
+  })
+  t.ok(downloadSpy.calledWith({
+    cache: mockEnv.electron_config_cache,
+    version: require('../../package').version.replace(/-.*/, ''),
+    platform: mockEnv.npm_config_platform,
+    arch: mockEnv.npm_config_arch,
+    strictSSL: mockEnv.npm_config_strict_ssl === 'true',
+    force: mockEnv.force_no_cache === 'true',
+    quiet: false
+  }), 'electron-download is called with correct options')
+
+  t.end()
+})
+
+tape('fails for unsupported platforms', (t) => {
+  sandbox.restore()
+  sandbox.stub(process, 'env').value(
+    Object.assign(mockEnv, { npm_config_platform: 'android' })
+  )
+  t.throws(() => {
+    proxyquire(path.join(__dirname, '..', 'install.js'), {
+      'electron-download': sinon.spy()
+    })
+  },
+  /Electron builds are not available on platform: android/i,
+  'install fails for unsupported platforms')
+  t.end()
+})
+
+tape('extract file', (t) => {
+
+  sandbox.restore()
+
+  sandbox.stub(process, 'env').value(
+    Object.assign(mockEnv, { npm_config_platform: 'darwin' })
+  )
+
+  // add file directly
+  const zip = new admZip()
+  zip.addFile('test.txt', Buffer.from('electron install test'))
+  zip.writeZip(path.join(tempDir, 'test.zip'))
+
+  // create fake zip
+  // mock download() returning path to fake zip
+
+  proxyquire(path.join(__dirname, '..', 'install.js'), {
+    'electron-download': (opts, cb) => cb(null, path.join(tempDir, 'test.zip'))
+  })
+
+  // call through to extractFile()
+  // check `/path.txt` to contain platformPath
   t.end()
 })
 
-tape('has cache folder', function (t) {
-  t.ok(pathExists.sync(path.join(getHomePath, './.electron')), 'cache exists')
+tape('teardown', (t) => {
+  // remove files
   t.end()
 })

+ 15 - 0
npm/test/test.js

@@ -0,0 +1,15 @@
+var tape = require('tape')
+var electron = require('../')
+var path = require('path')
+var pathExists = require('path-exists')
+var getHomePath = require('home-path')()
+
+tape('has local binary', function (t) {
+  t.ok(pathExists.sync(electron), 'electron was downloaded')
+  t.end()
+})
+
+tape('has cache folder', function (t) {
+  t.ok(pathExists.sync(path.join(getHomePath, './.electron')), 'cache exists')
+  t.end()
+})