diff --git a/.npmignore b/.npmignore deleted file mode 100644 index 93a3275..0000000 --- a/.npmignore +++ /dev/null @@ -1,5 +0,0 @@ -.nyc_output/ -.idea/ -test/ -.publishrc -.travis.yml \ No newline at end of file diff --git a/README.md b/README.md index b80f31e..ecab0ac 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,11 @@ Parse [clover](https://www.atlassian.com/software/clover) report files, and retu ## Usage 1. Install -`npm i @technote-space/clover-json` -1. + * npm + `npm i @technote-space/clover-json` + * yarn + `yarn add @technote-space/clover-json` +1. Use ```typescript import { parseFile, parseContent } from "@technote-space/clover-json"; diff --git a/__tests__/assets/clover-empty.xml b/__tests__/assets/clover-empty.xml index b09540a..965def0 100644 --- a/__tests__/assets/clover-empty.xml +++ b/__tests__/assets/clover-empty.xml @@ -2,7 +2,7 @@ - + @@ -11,4 +11,4 @@ - \ No newline at end of file + diff --git a/__tests__/assets/clover-without-class.xml b/__tests__/assets/clover-without-class.xml new file mode 100644 index 0000000..1fafe80 --- /dev/null +++ b/__tests__/assets/clover-without-class.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/__tests__/assets/clover-without-package.xml b/__tests__/assets/clover-without-package.xml index 45f916c..1fc2364 100644 --- a/__tests__/assets/clover-without-package.xml +++ b/__tests__/assets/clover-without-package.xml @@ -1,11 +1,11 @@ - + - + @@ -22,7 +22,7 @@ - + @@ -58,7 +58,7 @@ - + diff --git a/__tests__/assets/clover2.xml b/__tests__/assets/clover2.xml index 88e2060..3d8e3ba 100644 --- a/__tests__/assets/clover2.xml +++ b/__tests__/assets/clover2.xml @@ -2,7 +2,7 @@ - + @@ -17,4 +17,4 @@ - \ No newline at end of file + diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 5a5dd48..bed31a4 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -7,7 +7,6 @@ describe('Check if it can parse a clover file', () => { it('should parse a normal file', async() => { const result = await parseFile(getFilePath('clover1.xml')); - expect(result.length).toBe(3); expect(result[0].file).toBe('coveralls/lib/client.js'); expect(result[1].file).toBe('coveralls/lib/configuration.js'); @@ -24,6 +23,7 @@ describe('Check if it can parse a clover file', () => { it('should parse a second file', async() => { const result = await parseFile(getFilePath('clover2.xml')); + expect(result[0].file).toBe('hello.world/src/HelloWorld.php'); expect(result.length).toBe(1); expect(result[0].functions.found).toBe(2); expect(result[0].functions.hit).toBe(1); @@ -34,6 +34,7 @@ describe('Check if it can parse a clover file', () => { it('should parse a file with an empty class', async() => { const result = await parseFile(getFilePath('clover-empty.xml')); expect(result.length).toBe(1); + expect(result[0].file).toBe('hello.world/app/Models/Scenario.php'); expect(result[0].functions.found).toBe(0); expect(result[0].functions.hit).toBe(0); expect(result[0].lines.found).toBe(0); @@ -43,12 +44,26 @@ describe('Check if it can parse a clover file', () => { it('should parse a file without a package property', async() => { const result = await parseFile(getFilePath('clover-without-package.xml')); expect(result.length).toBe(4); + expect(result[0].file).toBe('config.php'); + expect(result[1].file).toBe('jira.php'); + expect(result[2].file).toBe('mattermost.php'); + expect(result[3].file).toBe('translate.php'); expect(result[0].functions.found).toBe(0); expect(result[0].functions.hit).toBe(0); expect(result[0].lines.found).toBe(1); expect(result[0].lines.hit).toBe(1); }); + it('should parse a file without a class property', async() => { + const result = await parseFile(getFilePath('clover-without-class.xml')); + expect(result.length).toBe(1); + expect(result[0].file).toBe('index.ts'); + expect(result[0].functions.found).toBe(0); + expect(result[0].functions.hit).toBe(0); + expect(result[0].lines.found).toBe(10); + expect(result[0].lines.hit).toBe(10); + }); + it('should throw error if file not exists', async() => { await expect(parseFile(getFilePath('test'))).rejects.toThrow('no such file or directory'); }); diff --git a/package.json b/package.json index e9cc7bd..e3275e8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@technote-space/clover-json", - "version": "0.4.1", + "version": "0.4.2", "description": "Parse clover XML coverage reports to JSON, using the same format as lcov-parse", "author": "TobiLG", "contributors": [ diff --git a/src/index.ts b/src/index.ts index 09c5306..4c2f67b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import { promisify } from 'util'; import { parseString } from 'xml2js'; type coverageData = { file: { $; line }[] } -const getCoverageData = (projectObj): coverageData[] => Object.prototype.hasOwnProperty.call(projectObj, 'package') ? projectObj.package : [Object.assign({}, projectObj, {name: undefined})]; +const getCoverageData = (projectObj): coverageData[] => Object.prototype.hasOwnProperty.call(projectObj, 'package') ? projectObj.package : [Object.assign({}, projectObj, {$: Object.assign({}, projectObj.$, {name: undefined})})]; const getPackageName = (data): string => Object.prototype.hasOwnProperty.call(data, '$') && Object.prototype.hasOwnProperty.call(data.$, 'name') && data.$.name ? data.$.name + '/' : ''; type classDetail = { name?: string; fileName: string; lines?: { $: { type; name; num; count } }[] }