Browse Source

Add test for corrupt install error message

Kevin Sawicki 8 years ago
parent
commit
e3e05aa797
1 changed files with 40 additions and 16 deletions
  1. 40 16
      npm/test/errors.js

+ 40 - 16
npm/test/errors.js

@@ -3,7 +3,7 @@ var tape = require('tape')
 var path = require('path')
 var childProcess = require('child_process')
 
-tape('fails for unsupported platforms', function (t) {
+tape('install fails for unsupported platforms', function (t) {
   install({npm_config_platform: 'android'}, function (code, stderr) {
     t.notEqual(stderr.indexOf('Electron builds are not available on platform: android'), -1, 'has error message')
     t.notEqual(code, 0, 'exited with error')
@@ -11,7 +11,7 @@ tape('fails for unsupported platforms', function (t) {
   })
 })
 
-tape('fails for unknown architectures', function (t) {
+tape('install fails for unsupported architectures', function (t) {
   install({
     npm_config_arch: 'midcentury',
     npm_config_platform: 'win32',
@@ -25,8 +25,16 @@ tape('fails for unknown architectures', function (t) {
   })
 })
 
-var install = function (env, callback) {
-  removeVersionFile()
+tape('require fails for corrupted installs', function (t) {
+  cli(function (code, stderr) {
+    t.notEqual(stderr.indexOf('Electron failed to install correctly'), -1, 'has error message')
+    t.notEqual(code, 0, 'exited with error')
+    t.end()
+  })
+})
+
+function install (env, callback) {
+  var restoreFile = removeFile(path.join(__dirname, '..', 'dist', 'version'))
 
   var installPath = path.join(__dirname, '..', 'install.js')
   var installProcess = childProcess.fork(installPath, {
@@ -40,23 +48,39 @@ var install = function (env, callback) {
   })
 
   installProcess.on('close', function (code) {
-    restoreVersionFile()
+    restoreFile()
     callback(code, stderr)
   })
 }
 
-var versionPath = path.join(__dirname, '..', 'dist', 'version')
-var versionContents = null
-function removeVersionFile () {
-  if (fs.existsSync(versionPath)) {
-    versionContents = fs.readFileSync(versionPath)
-    fs.unlinkSync(versionPath)
-  }
+function cli (callback) {
+  var restoreFile = removeFile(path.join(__dirname, '..', 'path.txt'))
+
+  var cliPath = path.join(__dirname, '..', 'cli.js')
+  var cliProcess = childProcess.fork(cliPath, {
+    silent: true
+  })
+
+  var stderr = ''
+  cliProcess.stderr.on('data', function (data) {
+    stderr += data
+  })
+
+  cliProcess.on('close', function (code) {
+    restoreFile()
+    callback(code, stderr)
+  })
 }
 
-function restoreVersionFile () {
-  if (versionContents != null) {
-    fs.writeFileSync(versionPath, versionContents)
-    versionContents = null
+function removeFile (filePath) {
+  var contents = null
+  if (fs.existsSync(filePath)) {
+    contents = fs.readFileSync(filePath)
+    fs.unlinkSync(filePath)
+  }
+  return function restoreFile () {
+    if (contents != null) {
+      fs.writeFileSync(filePath, contents)
+    }
   }
 }