Skip to content

Commit 52803c7

Browse files
authored
Issue 22 (#27)
* Add support for absolute file paths * Add tests for -f flag * Make path joins portable for Windows support
1 parent fb2c960 commit 52803c7

File tree

3 files changed

+64
-20
lines changed

3 files changed

+64
-20
lines changed

.circleci/config.yml

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,14 @@ jobs:
2626
# Download and cache dependencies
2727
- restore_cache:
2828
keys:
29-
- v1-dependencies-{{ checksum "package.json" }}
30-
# fallback to using the latest cache if no exact match is found
31-
- v1-dependencies-
29+
- v1-dependencies-{{ checksum "npm-shrinkwrap.json" }}
3230

3331
- run: npm install
3432

3533
- save_cache:
3634
paths:
3735
- node_modules
38-
key: v1-dependencies-{{ checksum "package.json" }}
36+
key: v1-dependencies-{{ checksum "npm-shrinkwrap.json" }}
3937

4038
# run tests!
4139
- run: make test
@@ -83,9 +81,13 @@ jobs:
8381
# Must be absolute path or relative path from working_directory
8482
at: .
8583
- run:
86-
name: Run Linux binary (dry run)
84+
name: Run Linux binary -f (dry run)
85+
command: |
86+
out/codecov-linux -f /home/circleci/project/coverage/cobertura-coverage.xml -F linux -d -Z >> output_linux.txt
87+
- run:
88+
name: Run Linux binary auto-detect (dry run)
8789
command: |
88-
out/codecov-linux -F linux -d -Z > output_linux.txt
90+
out/codecov-linux -F linux -d -Z >> output_linux.txt
8991
- run:
9092
name: Run Linux binary (upload)
9193
command: |
@@ -125,9 +127,13 @@ jobs:
125127
- attach_workspace:
126128
at: .
127129
- run:
128-
name: Run MacOS binary (dry-run)
130+
name: Run MacOS binary -f (dry-run)
131+
command: |
132+
out/codecov-macos -f /Users/distiller/project/coverage/cobertura-coverage.xml -F macos -d -Z >> output_osx.txt
133+
- run:
134+
name: Run MacOS binary auto-detect (dry-run)
129135
command: |
130-
out/codecov-macos -F macos -d -Z > output_osx.txt
136+
out/codecov-macos -F macos -d -Z >> output_osx.txt
131137
- run:
132138
name: Run MacOS binary (upload)
133139
command: |
@@ -165,9 +171,16 @@ jobs:
165171
- attach_workspace:
166172
at: .
167173
- run:
168-
name: Run Windows binary (dry-run)
174+
name: Run Windows binary -f (dry-run)
175+
command: |
176+
dir C:\Users\circleci\project\coverage\
177+
attrib C:\Users\circleci\project\coverage\cobertura-coverage.xml
178+
.\out\codecov.exe -f C:\Users\circleci\project\coverage\cobertura-coverage.xml -F windows -d -Z >> output_win.txt
179+
shell: cmd.exe
180+
- run:
181+
name: Run Windows binary auto-detect (dry-run)
169182
command: |
170-
.\out\codecov.exe -F windows -d -Z > output_win.txt
183+
.\out\codecov.exe -F windows -d -Z >> output_win.txt
171184
shell: cmd.exe
172185
- run:
173186
name: Run Windows binary (upload)

src/helpers/files.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,18 @@ function getAllFiles(projectRoot, dirPath, arrayOfFiles) {
117117

118118
files.forEach(function(file) {
119119
if (
120-
fs.statSync(dirPath + "/" + file).isDirectory() &&
120+
fs.statSync(path.join(dirPath, file)).isDirectory() &&
121121
!isBlacklisted(projectRoot, file, manualBlacklist())
122122
) {
123123
arrayOfFiles = getAllFiles(
124124
projectRoot,
125-
dirPath + "/" + file,
125+
path.join(dirPath, file),
126126
arrayOfFiles
127127
);
128128
} else {
129129
if (!isBlacklisted(projectRoot, file, manualBlacklist())) {
130130
arrayOfFiles.push(
131-
`${path.join(dirPath.replace(projectRoot, "."), "/", file)}\n`
131+
`${path.join(dirPath.replace(projectRoot, "."), file)}\n`
132132
);
133133
}
134134
}
@@ -146,8 +146,7 @@ function readAllLines(filePath) {
146146

147147
function readCoverageFile(projectRoot, filePath) {
148148
try {
149-
const fileContents = fs.readFileSync(`${projectRoot}/${filePath}`);
150-
return fileContents;
149+
return fs.readFileSync(getFilePath(projectRoot, filePath));
151150
} catch (error) {
152151
throw new Error(`There was an error reading the coverage file: ${error}`);
153152
}
@@ -161,6 +160,20 @@ function fileHeader(filePath) {
161160
return `# path=${filePath}\n`;
162161
}
163162

163+
function getFilePath(projectRoot, filePath) {
164+
if (filePath.startsWith("./")
165+
|| filePath.startsWith("/")
166+
|| filePath.startsWith(".\\")
167+
|| filePath.startsWith(".\\")) {
168+
return filePath
169+
}
170+
if (projectRoot === ".") {
171+
return path.join(".", filePath)
172+
}
173+
return path.join(projectRoot, filePath)
174+
175+
}
176+
164177
module.exports = {
165178
readCoverageFile,
166179
getFileListing,
@@ -169,5 +182,6 @@ module.exports = {
169182
fetchGitRoot,
170183
parseGitIgnore,
171184
getCoverageFiles,
172-
coverageFilePatterns
185+
coverageFilePatterns,
186+
getFilePath
173187
};

test/helpers/files.test.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/*global expect */
1+
const {afterEach, describe, it} = require("@jest/globals");
2+
23
const td = require("testdouble")
34
const fs = require("fs");
45
const path = require("path");
56
const child_process = require("child_process")
6-
const process = require("process")
77
const fileHelpers = require("../../src/helpers/files");
88

99
describe("File Helpers", () => {
@@ -46,7 +46,7 @@ describe("File Helpers", () => {
4646
});
4747
it("can read a coverage report file", async () => {
4848
const readFileSync = td.replace(fs, 'readFileSync')
49-
td.when(readFileSync("./test-coverage-file.xml")).thenReturn("I am test coverage data")
49+
td.when(readFileSync("test-coverage-file.xml")).thenReturn("I am test coverage data")
5050
const reportContents = fileHelpers.readCoverageFile(
5151
".",
5252
"test-coverage-file.xml"
@@ -59,9 +59,26 @@ describe("File Helpers", () => {
5959
).toStrictEqual(["test/index.test.js", "test/providers/index.test.js"]);
6060
});
6161
describe("coverage file patterns", function() {
62-
it("conatins `jacoco*.xml`", function() {
62+
it("contains `jacoco*.xml`", function() {
6363
expect(fileHelpers.coverageFilePatterns()).toContain("jacoco*.xml");
6464
});
6565
});
66+
describe("getFilePath()", () => {
67+
it("should return path when file path has no starting slash", () => {
68+
expect(fileHelpers.getFilePath("/usr/", "coverage.xml")).toEqual("/usr/coverage.xml")
69+
})
70+
it("should return path when file path has no starting slash", () => {
71+
expect(fileHelpers.getFilePath("/usr", "coverage.xml")).toEqual("/usr/coverage.xml")
72+
})
73+
it("should return path when file path starts with a ./", () => {
74+
expect(fileHelpers.getFilePath("/usr/", "./coverage.xml")).toEqual("./coverage.xml")
75+
})
76+
it("should return path when project root is . and filepath does not start with ./ or /", () => {
77+
expect(fileHelpers.getFilePath(".", "coverage.xml")).toEqual("coverage.xml")
78+
})
79+
it("should return path when project root is . and filepath starts /", () => {
80+
expect(fileHelpers.getFilePath(".", "/usr/coverage.xml")).toEqual("/usr/coverage.xml")
81+
})
82+
})
6683
});
6784
});

0 commit comments

Comments
 (0)