Socialify

Folder ..

Viewing helper.js
71 lines (64 loc) • 1.5 KB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require('./support/globals')
const fs = require('fs')
const ps = require('ps-node')

const appPath = () => {
  switch (process.platform) {
    case 'darwin':
      return path.join(
        __dirname,
        '..',
        '.tmp',
        'mac',
        'Swifty.app',
        'Contents',
        'MacOS',
        'Swifty'
      )
    case 'linux':
      return path.join(__dirname, '..', '.tmp', 'linux-unpacked', 'swifty')
    default:
      throw 'Unsupported platform'
  }
}

const fixturePath = file => {
  return path.join(process.cwd(), 'test', 'fixtures', file)
}

const storageFile = () => {
  return path.join(process.cwd(), 'test', '.storage', 'default.pwmx')
}

const prepareStorage = storage => {
  try {
    let data = ''
    if (storage !== 'pristine') {
      data = fs.readFileSync(fixturePath(`${storage}.txt`)).toString('utf-8')
    }
    fs.writeFileSync(storageFile(), data, { flag: 'w' })
  } catch (error) {
    console.log(error)
  }
}

const beforeHelper = options => {
  if (options && options.storage) prepareStorage(options.storage)
  global.app = new Application({
    path: appPath(),
    env: {
      SPECTRON_STORAGE_PATH: storageFile(),
      RUNNING_IN_SPECTRON: 1,
      APP_ENV: 'test'
    }
  })
  return app.start()
}

const afterHelper = () => {
  ps.lookup({ command: /MacOS\/Swifty$/ }, (err, items) => {
    items.forEach(item => {
      ps.kill(item.pid, err => {
        if (err) throw new Error(err)
      })
    })
  })
  if (app && app.isRunning()) {
    return app.stop()
  }
}

module.exports = { beforeHelper, afterHelper }