Skip to content

Commit

Permalink
Add Jest snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
charpeni committed Apr 6, 2019
1 parent ff171ff commit 5d452d0
Show file tree
Hide file tree
Showing 16 changed files with 2,481 additions and 105 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.DS_STORE
5 changes: 5 additions & 0 deletions __tests__/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"env": {
"jest": true
}
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UDID</key>
<string>148019A3-566F-4C21-A4FC-6FD75D9F051E</string>
<key>deviceType</key>
<string>com.apple.CoreSimulator.SimDeviceType.iPhone-7</string>
<key>isDeleted</key>
<false/>
<key>name</key>
<string>iPhone 7</string>
<key>runtime</key>
<string>com.apple.CoreSimulator.SimRuntime.iOS-12-1</string>
<key>state</key>
<integer>3</integer>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UDID</key>
<string>1E756F87-FF14-45C0-9775-A4469F72599E</string>
<key>deviceType</key>
<string>com.apple.CoreSimulator.SimDeviceType.iPad-Air</string>
<key>isDeleted</key>
<false/>
<key>name</key>
<string>iPad Air</string>
<key>runtime</key>
<string>com.apple.CoreSimulator.SimRuntime.iOS-9-3</string>
<key>state</key>
<integer>3</integer>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UDID</key>
<string>7BBD33B7-63B0-4D96-9BE0-BDCA97FE89E0</string>
<key>deviceType</key>
<string>com.apple.CoreSimulator.SimDeviceType.iPhone-6s</string>
<key>name</key>
<string>iPhone 6s</string>
<key>runtime</key>
<string>com.apple.CoreSimulator.SimRuntime.iOS-11-4</string>
<key>state</key>
<integer>1</integer>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UDID</key>
<string>C321F874-FA51-4DC5-8B9A-2E6E095D8C90</string>
<key>deviceType</key>
<string>com.apple.CoreSimulator.SimDeviceType.iPhone-XR</string>
<key>isDeleted</key>
<false/>
<key>name</key>
<string>iPhone XR</string>
<key>runtime</key>
<string>com.apple.CoreSimulator.SimRuntime.iOS-12-1</string>
<key>state</key>
<integer>3</integer>
</dict>
</plist>
76 changes: 76 additions & 0 deletions __tests__/__snapshots__/getAppsByDevice.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should get devices correctly 1`] = `
Array [
Object {
"UDID": "148019A3-566F-4C21-A4FC-6FD75D9F051E",
"apps": Array [
Object {
"CFBundleDisplayName": "pasta",
"CFBundleIdentifier": "org.reactjs.native.example.pasta",
"CFBundleShortVersionString": "3.0",
"CFBundleVersion": "8",
"file": "/home/circleci/ios-simulator-app-exporter/__tests__/Devices/148019A3-566F-4C21-A4FC-6FD75D9F051E/data/Containers/Bundle/Application/43FB53D5-6F42-4F38-A810-6C1A28B393E9/example.app",
},
Object {
"CFBundleDisplayName": "example",
"CFBundleIdentifier": "org.reactjs.native.example.example",
"CFBundleShortVersionString": "1.0",
"CFBundleVersion": "1",
"file": "/home/circleci/ios-simulator-app-exporter/__tests__/Devices/148019A3-566F-4C21-A4FC-6FD75D9F051E/data/Containers/Bundle/Application/950806E1-3259-4089-8EF3-DF0941E3B5B7/example.app",
},
],
"deviceType": "com.apple.CoreSimulator.SimDeviceType.iPhone-7",
"isDeleted": false,
"name": "iPhone 7",
"path": "/home/circleci/ios-simulator-app-exporter/__tests__/Devices/148019A3-566F-4C21-A4FC-6FD75D9F051E",
"runtime": "com.apple.CoreSimulator.SimRuntime.iOS-12-1",
"state": 3,
},
Object {
"UDID": "1E756F87-FF14-45C0-9775-A4469F72599E",
"apps": Array [
Object {
"CFBundleDisplayName": "example",
"CFBundleIdentifier": "org.reactjs.native.example.example",
"CFBundleShortVersionString": "1.0",
"CFBundleVersion": "1",
"file": "/home/circleci/ios-simulator-app-exporter/__tests__/Devices/1E756F87-FF14-45C0-9775-A4469F72599E/data/Containers/Bundle/Application/B3674201-2F90-4759-988E-CDB42181FD80/example.app",
},
],
"deviceType": "com.apple.CoreSimulator.SimDeviceType.iPad-Air",
"isDeleted": false,
"name": "iPad Air",
"path": "/home/circleci/ios-simulator-app-exporter/__tests__/Devices/1E756F87-FF14-45C0-9775-A4469F72599E",
"runtime": "com.apple.CoreSimulator.SimRuntime.iOS-9-3",
"state": 3,
},
Object {
"UDID": "7BBD33B7-63B0-4D96-9BE0-BDCA97FE89E0",
"apps": Array [],
"deviceType": "com.apple.CoreSimulator.SimDeviceType.iPhone-6s",
"name": "iPhone 6s",
"path": "/home/circleci/ios-simulator-app-exporter/__tests__/Devices/7BBD33B7-63B0-4D96-9BE0-BDCA97FE89E0",
"runtime": "com.apple.CoreSimulator.SimRuntime.iOS-11-4",
"state": 1,
},
Object {
"UDID": "C321F874-FA51-4DC5-8B9A-2E6E095D8C90",
"apps": Array [
Object {
"CFBundleDisplayName": "example",
"CFBundleIdentifier": "org.reactjs.native.example.example",
"CFBundleShortVersionString": "1.0",
"CFBundleVersion": "1",
"file": "/home/circleci/ios-simulator-app-exporter/__tests__/Devices/C321F874-FA51-4DC5-8B9A-2E6E095D8C90/data/Containers/Bundle/Application/4FEF6828-573D-4ACA-922D-85C1EEA7E509/example.app",
},
],
"deviceType": "com.apple.CoreSimulator.SimDeviceType.iPhone-XR",
"isDeleted": false,
"name": "iPhone XR",
"path": "/home/circleci/ios-simulator-app-exporter/__tests__/Devices/C321F874-FA51-4DC5-8B9A-2E6E095D8C90",
"runtime": "com.apple.CoreSimulator.SimRuntime.iOS-12-1",
"state": 3,
},
]
`;
5 changes: 5 additions & 0 deletions __tests__/getAppsByDevice.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const getAppsByDevice = require('../getAppsByDevice');

it('should get devices correctly', () => {
expect(getAppsByDevice(`${__dirname}/Devices`)).toMatchSnapshot();
});
70 changes: 70 additions & 0 deletions getAppsByDevice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const fs = require('fs');
const { join } = require('path');
const { homedir } = require('os');
const plist = require('simple-plist');

const DEVICES_PATH = '/Library/Developer/CoreSimulator/Devices/';
const APPS_PATH = '/data/Containers/Bundle/Application';

function getDirectories(source) {
if (!fs.existsSync(source)) {
return [];
}

return fs
.readdirSync(source)
.map(name => join(source, name))
.filter(file => fs.lstatSync(file).isDirectory());
}

function getAppsByDevice(searchingPath = homedir() + DEVICES_PATH) {
return getDirectories(searchingPath).map(devicePath => {
const device = plist.readFileSync(`${devicePath}/device.plist`);

return {
path: devicePath,
...device,
apps: getDirectories(devicePath + APPS_PATH).reduce((acc, appPath) => {
const apps = getDirectories(appPath)
.filter(file => file.endsWith('.app'))
.map(file => {
if (fs.existsSync(`${file}/Info.plist`)) {
let app;

try {
app = plist.readFileSync(`${file}/Info.plist`);
} catch (error) {
console.error(`Something is wrong with ${file}: ${error}`);
}

if (!app) {
return undefined;
}

const {
CFBundleDisplayName,
CFBundleIdentifier,
CFBundleShortVersionString,
CFBundleVersion,
} = app;

return {
file,
CFBundleDisplayName,
CFBundleIdentifier,
CFBundleShortVersionString,
CFBundleVersion,
};
}

return undefined;
})
.filter(app => app);

return [...acc, ...apps];
}, []),
};
});
}

module.exports = getAppsByDevice;
72 changes: 1 addition & 71 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,7 @@
const fs = require('fs');
const { join } = require('path');
const { homedir } = require('os');
const plist = require('simple-plist');
const inquirer = require('inquirer');
const chalk = require('chalk');

const DEVICES_PATH = '/Library/Developer/CoreSimulator/Devices/';
const APPS_PATH = '/data/Containers/Bundle/Application';

function getDirectories(source) {
if (!fs.existsSync(source)) {
return [];
}

return fs
.readdirSync(source)
.map(name => join(source, name))
.filter(file => fs.lstatSync(file).isDirectory());
}

function getDevices(searchingPath = homedir() + DEVICES_PATH) {
return getDirectories(searchingPath).map(devicePath => {
const device = plist.readFileSync(`${devicePath}/device.plist`);

return {
path: devicePath,
...device,
apps: getDirectories(devicePath + APPS_PATH).reduce((acc, appPath) => {
const apps = getDirectories(appPath)
.filter(file => file.endsWith('.app'))
.map(file => {
if (fs.existsSync(`${file}/Info.plist`)) {
let app;

try {
app = plist.readFileSync(`${file}/Info.plist`);
} catch (error) {
console.error(`Something is wrong with ${file}: ${error}`);
}

if (!app) {
return undefined;
}

const {
CFBundleDisplayName,
CFBundleIdentifier,
CFBundleShortVersionString,
CFBundleVersion,
} = app;

return {
file,
CFBundleDisplayName,
CFBundleIdentifier,
CFBundleShortVersionString,
CFBundleVersion,
};
}

return undefined;
})
.filter(app => app);

return [...acc, ...apps];
}, []),
};
});
}
const getDevices = require('./getAppsByDevice');

function activeDevicesOnlyFilter(device) {
return device.state === 3;
Expand Down Expand Up @@ -172,7 +106,3 @@ async function main() {
}

main();

module.exports = {
getDevices,
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1",
"test": "jest",
"precommit": "lint-staged"
},
"author": "Nicolas Charpentier <[email protected]>",
Expand All @@ -22,6 +22,7 @@
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.1",
"eslint-plugin-react": "^7.11.0",
"jest": "^23.6.0",
"lint-staged": "^8.1.0",
"prettier": "1.15.3"
},
Expand Down
Loading

0 comments on commit 5d452d0

Please sign in to comment.