Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testPaths is not relative to basePath, unlike outputFile #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,13 @@ var SonarQubeUnitReporter = function (baseReporterDecorator, config, logger, hel
var testPath = reporterConfig.testPath || './'
var testPaths = reporterConfig.testPaths || [testPath]
var testFilePattern = reporterConfig.testFilePattern || '(.spec.ts|.spec.js)'
var filesForDescriptions = fileUtil.getFilesForDescriptions(testPaths, testFilePattern)
var filesForDescriptions

if (reporterConfig.basePathGlob) {
filesForDescriptions = fileUtil.globFilesForDescriptions(reporterConfig.basePathGlob, config.basePath)
} else {
filesForDescriptions = fileUtil.getFilesForDescriptions(testPaths, testFilePattern)
}

function defaultFilenameFormatter (nextPath, result) {
return filesForDescriptions[nextPath]
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"eslint-config-standard": "^4.1.0",
"eslint-plugin-jasmine": "2.2.0",
"eslint-plugin-standard": "^1.3.1",
"glob": "^7.1.3",
"grunt": "^0.4.1",
"grunt-bump": "^0.5.0",
"grunt-conventional-changelog": "^4.1.0",
Expand Down
63 changes: 38 additions & 25 deletions src/file-util.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,55 @@
var path = require('path')
var fs = require('fs')
var glob = require('glob')

module.exports = {
getFilesForDescriptions: getFilesForDescriptions
getFilesForDescriptions: getFilesForDescriptions,
globFilesForDescriptions: globFilesForDescriptions
}

function getFilesForDescriptions (startPaths, filter) {
var ret = {}
var result = {}

startPaths.forEach(function (startPathItem) {
var files = findFilesInDir(startPathItem, filter)
files.forEach(findDescriptionInFile)
findFilesInDir(startPathItem, filter).forEach(function (file) {
insertDescriptionsForFile(result, file, '')
})
})

function findDescriptionInFile (item, index) {
try {
var fileText = fs.readFileSync(item, 'utf8')
var position = 0
while (position !== -1) {
position = fileText.indexOf('describe(')
if (position !== -1) {
var delimeter = fileText[position + 9]
var descriptionEnd = fileText.indexOf(delimeter, position + 10) + 1
var describe = fileText.substring(position + 10, descriptionEnd - 1)
describe = describe.replace(/\\\\/g, '/')
item = item.replace(/\\\\/g, '/').replace(/\\/g, '/')
ret[describe] = item
position = 0
fileText = fileText.substring(descriptionEnd)
}
console.log('-- describe: ' + describe + ' -> file: ' + item)
return result
}

function globFilesForDescriptions (filesGlob, basePath) {
var result = {}

glob.sync(path.join(basePath, filesGlob)).forEach(function (file) {
insertDescriptionsForFile(result, file, basePath)
})

return result
}

function insertDescriptionsForFile (descriptions, file, basePath) {
try {
var fileText = fs.readFileSync(file, 'utf8')
var position = 0
file = path.relative(basePath, file)
while (position !== -1) {
position = fileText.indexOf('describe(')
if (position !== -1) {
var delimeter = fileText[position + 9]
var descriptionEnd = fileText.indexOf(delimeter, position + 10) + 1
var describe = fileText.substring(position + 10, descriptionEnd - 1)
describe = describe.replace(/\\\\/g, '/')
descriptions[describe] = file.replace(/\\\\/g, '/').replace(/\\/g, '/')
position = 0
fileText = fileText.substring(descriptionEnd)
}
} catch (e) {
console.log('Error:', e.stack)
console.log('-- describe: ' + describe + ' -> file: ' + file)
}
} catch (e) {
console.log('Error:', e.stack)
}

return ret
}

function findFilesInDir (startPath, filter) {
Expand Down
19 changes: 19 additions & 0 deletions test/spec/file-util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,23 @@ describe('create description - file name map from test sources', function () {
}
expect(filesForDescriptions).toEqual(expected)
})

it('globs many files', function () {
var filesForDescriptions = fileUtil.globFilesForDescriptions('multiple_files_*/*.spec.js', 'test/resources')
var expectedPaths = [
'multiple_files_one_description/first_test.spec.js',
'multiple_files_one_description/second_test.spec.js',
'multiple_files_multiple_descriptions/first_test.spec.js',
'multiple_files_multiple_descriptions/second_test.spec.js'
]
var expected = {
'first test description': expectedPaths[0],
'second test description': expectedPaths[1],
'first test first description': expectedPaths[2],
'first test second description': expectedPaths[2],
'second test first description': expectedPaths[3],
'second test second description': expectedPaths[3]
}
expect(filesForDescriptions).toEqual(expected)
})
})