Skip to content

Commit

Permalink
Refactor and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
qtomlinson committed Feb 5, 2024
1 parent 6eead81 commit ca3901a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
10 changes: 9 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: MIT
const { DateTime } = require('luxon')
const { spawn } = require('child_process')
const { intersection } = require('lodash')

const dateTimeFormats = [
'EEE MMM d HH:mm:ss \'GMT\'ZZ yyyy' //in pom properties
Expand Down Expand Up @@ -31,6 +32,13 @@ function trimAllParents(paths, parents) {
return paths.map(path => trimParents(path, parents))
}

function isValidExcludingGit(file) {
const exclusions = ['.git']
if (!file) return false
const segments = file.split(/[\\/]/g)
return !intersection(segments, exclusions).length
}

function extractDate(dateAndTime, formats = dateTimeFormats) {
if (!dateAndTime) return dateAndTime
let luxonResult = DateTime.fromISO(dateAndTime)
Expand Down Expand Up @@ -75,4 +83,4 @@ function spawnPromisified(command, args, options) {
})
}

module.exports = { normalizePath, normalizePaths, trimParents, trimAllParents, extractDate, spawnPromisified }
module.exports = { normalizePath, normalizePaths, trimParents, trimAllParents, isValidExcludingGit, extractDate, spawnPromisified }
4 changes: 2 additions & 2 deletions providers/process/abstractClearlyDefinedProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const throat = require('throat')
const path = require('path')
const { pick, merge } = require('lodash')
const du = require('du')
const { trimParents } = require('../../lib/utils')
const { trimParents, isValidExcludingGit } = require('../../lib/utils')

class AbstractClearlyDefinedProcessor extends AbstractProcessor {
get toolVersion() {
Expand Down Expand Up @@ -75,7 +75,7 @@ class AbstractClearlyDefinedProcessor extends AbstractProcessor {
let count = 0
const bytes = await du(location, {
filter: file => {
if (!this.isValidExcludingGit(file)) return false
if (!isValidExcludingGit(file)) return false
count++
return true
}
Expand Down
17 changes: 3 additions & 14 deletions providers/process/abstractProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const EntitySpec = require('../../lib/entitySpec')
const fs = require('fs')
const path = require('path')
const shajs = require('sha.js')
const { clone, flatten, intersection, pick, set } = require('lodash')
const { clone, flatten, pick, set } = require('lodash')
const { promisify } = require('util')
const readdir = promisify(fs.readdir)
const lstat = promisify(fs.lstat)
const { trimAllParents } = require('../../lib/utils')
const { trimAllParents, isValidExcludingGit } = require('../../lib/utils')

class AbstractProcessor extends BaseHandler {
constructor(options) {
Expand Down Expand Up @@ -150,21 +150,10 @@ class AbstractProcessor extends BaseHandler {
*/
async filterFiles(location) {
const fullList = await this.getFiles(location)
const filteredList = fullList.filter(file => this.isValidExcludingGit(file))
const filteredList = fullList.filter(file => isValidExcludingGit(file))
return trimAllParents(filteredList, location).filter(x => x)
}

_isValid(file, exclusions) {
if (!file) return false
const segments = file.split(/[\\/]/g)
return !intersection(segments, exclusions).length
}

isValidExcludingGit(file) {
const exclusions = ['.git']
return this._isValid(file, exclusions)
}

shouldFetch() {
return true
}
Expand Down
18 changes: 17 additions & 1 deletion test/unit/lib/utilsTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
const { normalizePath, normalizePaths, trimParents, trimAllParents, extractDate, spawnPromisified } = require('../../../lib/utils')
const { normalizePath, normalizePaths, trimParents, trimAllParents, extractDate, spawnPromisified, isValidExcludingGit } = require('../../../lib/utils')
const { promisify } = require('util')
const execFile = promisify(require('child_process').execFile)
chai.use(chaiAsPromised)
Expand Down Expand Up @@ -56,6 +56,22 @@ describe('Utils path functions', () => {
})
})

describe('Util isValidExcludingGit', () => {
it('should exclude .git and its contents', () => {
const data = new Map([
[null, false],
['/', true],
['/tmp/tempX/package/src', true],
['.git', false],
['/tmp/tempX/package/.git', false],
['/tmp/tempX/package/.git/hooks/pre-merge-commit.sample', false]
])
data.forEach((expected, input) => {
expect(isValidExcludingGit(input)).to.eq(expected)
})
})
})

describe('Util extractDate', () => {
it('handle null', () => {
expect(extractDate(null)).to.be.null
Expand Down

0 comments on commit ca3901a

Please sign in to comment.