<commit_before>'use strict'

var path = require('path')
var fs = require('fs')
var childProcess = require('child_process')
var should = require('should')
var utils = require('../utils')
var install = require('../../lib/commands/install').handler

var IS_WINDOWS = process.platform === 'win32'

var TEMP_DIRS = [
  'install-empty',
  'install-packagejson-only',
  'install-packagejson-ok'
]

describe('install command', function () {
  // disabling timeout because removing files could take a
  // couple of seconds
  this.timeout(0)

  before(function () {
    utils.cleanTempDir(TEMP_DIRS)

    utils.createTempDir(TEMP_DIRS, function (dir, absoluteDir) {
      switch (dir) {
        case 'install-packagejson-only':
          fs.writeFileSync(
            path.join(absoluteDir, './package.json'),
            JSON.stringify({
              description: 'example project'
            }, null, 2)
          )
          return

        case 'install-packagejson-without-entry':
          fs.writeFileSync(
            path.join(absoluteDir, './package.json'),
            JSON.stringify({
              name: 'example project'
            }, null, 2)
          )

          return

        case 'install-packagejson-ok':
          fs.writeFileSync(
            path.join(absoluteDir, './package.json'),
            JSON.stringify({
              name: 'jsreport-server-for-cli-testing',
              main: 'server.js',
              dependencies: {
                jsreport: '*'
              }
            }, null, 2)
          )

          fs.writeFileSync(
            path.join(absoluteDir, './server.js'),
            'require("jsreport")().init().catch(function(err) { console.error("Error starting jsreport:", err) })'
          )
          return
      }
    })
  })

  it('should not work on empty directory', function () {
    var dir = utils.getTempDir('install-empty')
    var installAsync = install({ context: { cwd: dir } })

    if (!IS_WINDOWS) {
      should(installAsync).be.fulfilledWith({ installed: false })
    } else {
      should(installAsync).be.rejected()
    }
  })

  it('should not work on directory with package.json without name field', function () {
    var dir = utils.getTempDir('install-packagejson-only')
    var installAsync = install({ context: { cwd: dir } })

    if (!IS_WINDOWS) {
      should(installAsync).be.fulfilledWith({ installed: false })
    } else {
      should(installAsync).be.rejected()
    }
  })

  it('should not work on directory with package.json without main or scripts field', function () {
    var dir = utils.getTempDir('install-packagejson-without-entry')
    var installAsync = install({ context: { cwd: dir } })

    if (!IS_WINDOWS) {
      should(installAsync).be.fulfilledWith({ installed: false })
    } else {
      should(installAsync).be.rejected()
    }
  })

  it('should install windows service', function (done) {
    // disabling timeout because npm install could take a
    // couple of minutes
    this.timeout(0)

    var dir = utils.getTempDir('install-packagejson-ok')
    var installAsync

    console.log('installing jsreport for the test case...')

    childProcess.exec('npm install', {
      cwd: dir
    }, function (error, stdout, stderr) {
      if (error) {
        return done(error)
      }

      installAsync = install({ context: { cwd: dir } })<commit_msg>fix install commands tests on non-windows systems<commit_after>'use strict'

var path = require('path')
var fs = require('fs')
var childProcess = require('child_process')
var should = require('should')
var utils = require('../utils')
var install = require('../../lib/commands/install').handler

var IS_WINDOWS = process.platform === 'win32'

var TEMP_DIRS = [
  'install-empty',
  'install-packagejson-only',
  'install-packagejson-ok'
]

describe.only('install command', function () {
  // disabling timeout because removing files could take a
  // couple of seconds
  this.timeout(0)

  before(function () {
    utils.cleanTempDir(TEMP_DIRS)

    utils.createTempDir(TEMP_DIRS, function (dir, absoluteDir) {
      switch (dir) {
        case 'install-packagejson-only':
          fs.writeFileSync(
            path.join(absoluteDir, './package.json'),
            JSON.stringify({
              description: 'example project'
            }, null, 2)
          )
          return

        case 'install-packagejson-without-entry':
          fs.writeFileSync(
            path.join(absoluteDir, './package.json'),
            JSON.stringify({
              name: 'example project'
            }, null, 2)
          )

          return

        case 'install-packagejson-ok':
          fs.writeFileSync(
            path.join(absoluteDir, './package.json'),
            JSON.stringify({
              name: 'jsreport-server-for-cli-testing',
              main: 'server.js',
              dependencies: {
                jsreport: '*'
              }
            }, null, 2)
          )

          fs.writeFileSync(
            path.join(absoluteDir, './server.js'),
            'require("jsreport")().init().catch(function(err) { console.error("Error starting jsreport:", err) })'
          )
          return
      }
    })
  })

  it('should not work on empty directory', function () {
    var dir = utils.getTempDir('install-empty')
    var installAsync = install({ context: { cwd: dir } })

    if (!IS_WINDOWS) {
      should(installAsync).be.fulfilledWith({ installed: false, serviceName: null })
    } else {
      should(installAsync).be.rejected()
    }
  })

  it('should not work on directory with package.json without name field', function () {
    var dir = utils.getTempDir('install-packagejson-only')
    var installAsync = install({ context: { cwd: dir } })

    if (!IS_WINDOWS) {
      should(installAsync).be.fulfilledWith({ installed: false, serviceName: null })
    } else {
      should(installAsync).be.rejected()
    }
  })

  it('should not work on directory with package.json without main or scripts field', function () {
    var dir = utils.getTempDir('install-packagejson-without-entry')
    var installAsync = install({ context: { cwd: dir } })

    if (!IS_WINDOWS) {
      should(installAsync).be.fulfilledWith({ installed: false, serviceName: null })
    } else {
      should(installAsync).be.rejected()
    }
  })

  it('should install windows service', function (done) {
    // disabling timeout because npm install could take a
    // couple of minutes
    this.timeout(0)

    var dir = utils.getTempDir('install-packagejson-ok')
    var installAsync

    console.log('installing jsreport for the test case...')

    childProcess.exec('npm install', {
      cwd: dir
    }, function (error, stdout, stderr) {
      if (error) {
        return done(error)
      }

      installAsync = install({ context: { cwd: dir } })
{'fraction_non_alphanumeric': 0.13354857646022894, 'fraction_numerical': 0.002054593484003522, 'mean_word_length': 2.352188883423512, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '791806', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1919, 'n_words': 460}