Skip to content

Commit

Permalink
organize into the different lists
Browse files Browse the repository at this point in the history
  • Loading branch information
kanekotic committed Jul 29, 2018
1 parent f046868 commit 80ae608
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
22 changes: 15 additions & 7 deletions lib/dependency-shrink.js
@@ -1,10 +1,18 @@
const files = require('./helpers/files')
const files = require('./helpers/files'),
execute = require('./helpers/execute')

const shrink = (directories) => {
files.getAll(directories)
return {
optionals: [],
notOptionals: []
}
const shrink = async (executable, directories, recursive, bootTime) => {
let optionals = []
let notOptionals = []
let fileList = files.getAll(directories, recursive)
fileList.forEach(async file => {
let optional = await execute(executable, file, bootTime)
if(optional.isOptional)
optionals.push(file)
else
notOptionals.push(file)

});
return { optionals, notOptionals }
}
module.exports = shrink
23 changes: 17 additions & 6 deletions test/lib/dependency-shrink-test.js
@@ -1,8 +1,10 @@
jest.mock('../../lib/helpers/files', () => ({
getAll: jest.fn()
}))
jest.mock('../../lib/helpers/execute', () => jest.fn())
const shrinker = require('../../lib/dependency-shrink'),
files = require('../../lib/helpers/files'),
execute = require('../../lib/helpers/execute'),
faker = require('faker')

describe('list files should', () => {
Expand All @@ -13,15 +15,24 @@ describe('list files should', () => {
})

it('return a list of files that are optional and non optional',async () => {
let fileList = [faker.random.uuid(), faker.random.uuid()]
let fileList = [faker.random.uuid(), faker.random.uuid()],
bootTime = faker.random.number(1000)
executable = faker.random.uuid(),
recursive = faker.random.boolean()
files.getAll.mockReturnValue(fileList)
execute.mockReturnValueOnce(Promise.resolve({isOptional: false}))
execute.mockReturnValueOnce(Promise.resolve({isOptional: true}))

let result = shrinker(directories)

expect(files.getAll).toBeCalledWith(directories)
expect(result.optionals).toBeDefined()
expect(result.notOptionals).toBeDefined()
let result = await shrinker(executable, directories, recursive, bootTime)

expect(files.getAll).toBeCalledWith(directories, recursive)
fileList.forEach(file => {
expect(execute).toBeCalledWith(executable, file, bootTime)
})
expect(result).toEqual({
optionals:[fileList[1]],
notOptionals:[fileList[0]]
})
})

})
Expand Down
21 changes: 0 additions & 21 deletions test/lib/helpers/execute-test.js
Expand Up @@ -107,24 +107,3 @@ describe('execute should', () => {

})
})

// function testExecute(executable, file, boottime){
// return new Promise( (resolve, reject) => {
// let timeoutId;
// let localFile = file;
// fs.renameSync(localFile, localFile+'.back', (err) => { if (err) reject(err) })
// let child = exec(executable)
// child.on('exit', (code, signal) => {
// clearTimeout(timeoutId);
// fs.renameSync(localFile+'.back', localFile, (err) => { if (err) reject(err) })
// console.log(code)
// if (signal === 'SIGKILL' || code === 1)
// resolve({isOptional: true})
// else
// resolve({isOptional: false})
// });
// timeoutId = setTimeout(() => {
// kill(child.pid, 'SIGKILL')
// }, boottime)
// })
// }

0 comments on commit 80ae608

Please sign in to comment.