Skip to content

Commit

Permalink
added unit tests (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
shazron authored Feb 11, 2022
1 parent a82595e commit e749386
Show file tree
Hide file tree
Showing 13 changed files with 4,236 additions and 778 deletions.
7 changes: 7 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "standard",
"plugins": ["jest"],
"env": {
"jest": true
}
}
10 changes: 0 additions & 10 deletions .eslintrc.yml

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
coverage
40 changes: 0 additions & 40 deletions .jscsrc

This file was deleted.

12 changes: 12 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
testEnvironment: 'node',
verbose: true,
collectCoverage: true,
testPathIgnorePatterns: [
'<rootDir>/src/*'
],
collectCoverageFrom: [
'lib/**/*.js',
'simctl.js'
]
}
160 changes: 83 additions & 77 deletions lib/simctl-extensions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Shazron Abdullah.
Copyright (c) 2022 Shazron Abdullah.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,81 +22,87 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

var shell = require('shelljs');
var path = require('path');
var fs = require('fs');
var util = require('util');
var Tail = require('tail').Tail;

var extensions = {
start: function (deviceid) {
var is_at_least_xcode_9 = false;

var command = 'xcodebuild -version';
var output = shell.exec(command, { silent: true }).output;

// parse output for Xcode version
var versionMatch = /Xcode (.*)/.exec(output);
if (!versionMatch) {
console.log('Unable to parse xcodebuild version.');
return;
} else {
is_at_least_xcode_9 = (parseInt(versionMatch[1]) >= 9);
}

if (is_at_least_xcode_9) {
// Xcode 9 or greater
command = util.format('xcrun simctl list -j');
var res = shell.exec(command, { silent: true });
if (res.code !== 0) {
console.log('Could not get device list.');
return;
}
var list_output = JSON.parse(res.output);
var device = Object.keys(list_output.devices)
.reduce(function (acc, key) { return acc.concat(list_output.devices[key]); }, [])
.find(function (el) { return el.udid === deviceid; });

if (device.state === 'Booted') {
// no need to launch the emulator, it is already running
console.log('Simulator already running.');
return;
}
command = util.format('xcrun simctl boot "%s"', deviceid);
shell.exec(command, { silent: true });
command = 'open `xcode-select -p`/Applications/Simulator.app';
return shell.exec(command, { silent: true });
} else {
// Xcode 8 or older
command = util.format('xcrun instruments -w "%s"', deviceid);
return shell.exec(command, { silent: true });
}
},

log: function (deviceid, filepath) {
var tail = new Tail(
path.join(process.env.HOME, 'Library/Logs/CoreSimulator', deviceid, 'system.log')
);

tail.on('line', function (data) {
if (filepath) {
fs.appendFile(filepath, data + '\n', function (error) {
if (error) {
console.error('ERROR: ', error);
throw error;
}
});
} else {
console.log(data);
}
});

tail.on('error', function (error) {
console.error('ERROR: ', error);
});

return tail;
const shell = require('shelljs')
const path = require('path')
const fs = require('fs')
const util = require('util')
const Tail = require('tail').Tail

const extensions = {
start: function (deviceid) {
let isAtLeastXcode9 = false

let command = 'xcodebuild -version'
const res = shell.exec(command, { silent: true })

// parse output for Xcode version
const versionMatch = /Xcode (.*)/.exec(res.output)
if (res.code !== 0 || !versionMatch) {
console.error('Unable to parse xcodebuild version.')
return
} else {
isAtLeastXcode9 = (parseInt(versionMatch[1]) >= 9)
}

if (isAtLeastXcode9) {
// Xcode 9 or greater
command = util.format('xcrun simctl list -j')
let res = shell.exec(command, { silent: true })
if (res.code !== 0) {
console.error('Could not get device list.')
return
}
const listOutput = JSON.parse(res.output)
const device = Object.keys(listOutput.devices)
.reduce(function (acc, key) { return acc.concat(listOutput.devices[key]) }, [])
.find(function (el) { return el.udid === deviceid })

if (device.state === 'Booted') {
// no need to launch the emulator, it is already running
console.error('Simulator already running.')
return
}
command = util.format('xcrun simctl boot "%s"', deviceid)
res = shell.exec(command, { silent: true })

if (res.code !== 0) {
console.error(`Could not boot simulator ${deviceid}`)
return
}

command = 'open `xcode-select -p`/Applications/Simulator.app'
return shell.exec(command, { silent: true })
} else {
// Xcode 8 or older
command = util.format('xcrun instruments -w "%s"', deviceid)
return shell.exec(command, { silent: true })
}
};
},

log: function (deviceid, filepath) {
const tail = new Tail(
path.join(process.env.HOME, 'Library/Logs/CoreSimulator', deviceid, 'system.log')
)

tail.on('line', function (data) {
if (filepath) {
fs.appendFile(filepath, data + '\n', function (error) {
if (error) {
console.error('ERROR: ', error)
throw error
}
})
} else {
console.log(data)
}
})

tail.on('error', function (error) {
console.error('ERROR: ', error)
})

return tail
}
}

exports = module.exports = extensions;
exports = module.exports = extensions
Loading

0 comments on commit e749386

Please sign in to comment.