Skip to content

Commit

Permalink
Issue 22 (#27)
Browse files Browse the repository at this point in the history
* Add support for absolute file paths

* Add tests for -f flag

* Make path joins portable for Windows support
  • Loading branch information
drazisil authored Jun 11, 2020
1 parent fb2c960 commit 52803c7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 20 deletions.
33 changes: 23 additions & 10 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ jobs:
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- v1-dependencies-{{ checksum "npm-shrinkwrap.json" }}

- run: npm install

- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
key: v1-dependencies-{{ checksum "npm-shrinkwrap.json" }}

# run tests!
- run: make test
Expand Down Expand Up @@ -83,9 +81,13 @@ jobs:
# Must be absolute path or relative path from working_directory
at: .
- run:
name: Run Linux binary (dry run)
name: Run Linux binary -f (dry run)
command: |
out/codecov-linux -f /home/circleci/project/coverage/cobertura-coverage.xml -F linux -d -Z >> output_linux.txt
- run:
name: Run Linux binary auto-detect (dry run)
command: |
out/codecov-linux -F linux -d -Z > output_linux.txt
out/codecov-linux -F linux -d -Z >> output_linux.txt
- run:
name: Run Linux binary (upload)
command: |
Expand Down Expand Up @@ -125,9 +127,13 @@ jobs:
- attach_workspace:
at: .
- run:
name: Run MacOS binary (dry-run)
name: Run MacOS binary -f (dry-run)
command: |
out/codecov-macos -f /Users/distiller/project/coverage/cobertura-coverage.xml -F macos -d -Z >> output_osx.txt
- run:
name: Run MacOS binary auto-detect (dry-run)
command: |
out/codecov-macos -F macos -d -Z > output_osx.txt
out/codecov-macos -F macos -d -Z >> output_osx.txt
- run:
name: Run MacOS binary (upload)
command: |
Expand Down Expand Up @@ -165,9 +171,16 @@ jobs:
- attach_workspace:
at: .
- run:
name: Run Windows binary (dry-run)
name: Run Windows binary -f (dry-run)
command: |
dir C:\Users\circleci\project\coverage\
attrib C:\Users\circleci\project\coverage\cobertura-coverage.xml
.\out\codecov.exe -f C:\Users\circleci\project\coverage\cobertura-coverage.xml -F windows -d -Z >> output_win.txt
shell: cmd.exe
- run:
name: Run Windows binary auto-detect (dry-run)
command: |
.\out\codecov.exe -F windows -d -Z > output_win.txt
.\out\codecov.exe -F windows -d -Z >> output_win.txt
shell: cmd.exe
- run:
name: Run Windows binary (upload)
Expand Down
26 changes: 20 additions & 6 deletions src/helpers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,18 @@ function getAllFiles(projectRoot, dirPath, arrayOfFiles) {

files.forEach(function(file) {
if (
fs.statSync(dirPath + "/" + file).isDirectory() &&
fs.statSync(path.join(dirPath, file)).isDirectory() &&
!isBlacklisted(projectRoot, file, manualBlacklist())
) {
arrayOfFiles = getAllFiles(
projectRoot,
dirPath + "/" + file,
path.join(dirPath, file),
arrayOfFiles
);
} else {
if (!isBlacklisted(projectRoot, file, manualBlacklist())) {
arrayOfFiles.push(
`${path.join(dirPath.replace(projectRoot, "."), "/", file)}\n`
`${path.join(dirPath.replace(projectRoot, "."), file)}\n`
);
}
}
Expand All @@ -146,8 +146,7 @@ function readAllLines(filePath) {

function readCoverageFile(projectRoot, filePath) {
try {
const fileContents = fs.readFileSync(`${projectRoot}/${filePath}`);
return fileContents;
return fs.readFileSync(getFilePath(projectRoot, filePath));
} catch (error) {
throw new Error(`There was an error reading the coverage file: ${error}`);
}
Expand All @@ -161,6 +160,20 @@ function fileHeader(filePath) {
return `# path=${filePath}\n`;
}

function getFilePath(projectRoot, filePath) {
if (filePath.startsWith("./")
|| filePath.startsWith("/")
|| filePath.startsWith(".\\")
|| filePath.startsWith(".\\")) {
return filePath
}
if (projectRoot === ".") {
return path.join(".", filePath)
}
return path.join(projectRoot, filePath)

}

module.exports = {
readCoverageFile,
getFileListing,
Expand All @@ -169,5 +182,6 @@ module.exports = {
fetchGitRoot,
parseGitIgnore,
getCoverageFiles,
coverageFilePatterns
coverageFilePatterns,
getFilePath
};
25 changes: 21 additions & 4 deletions test/helpers/files.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*global expect */
const {afterEach, describe, it} = require("@jest/globals");

const td = require("testdouble")
const fs = require("fs");
const path = require("path");
const child_process = require("child_process")
const process = require("process")
const fileHelpers = require("../../src/helpers/files");

describe("File Helpers", () => {
Expand Down Expand Up @@ -46,7 +46,7 @@ describe("File Helpers", () => {
});
it("can read a coverage report file", async () => {
const readFileSync = td.replace(fs, 'readFileSync')
td.when(readFileSync("./test-coverage-file.xml")).thenReturn("I am test coverage data")
td.when(readFileSync("test-coverage-file.xml")).thenReturn("I am test coverage data")
const reportContents = fileHelpers.readCoverageFile(
".",
"test-coverage-file.xml"
Expand All @@ -59,9 +59,26 @@ describe("File Helpers", () => {
).toStrictEqual(["test/index.test.js", "test/providers/index.test.js"]);
});
describe("coverage file patterns", function() {
it("conatins `jacoco*.xml`", function() {
it("contains `jacoco*.xml`", function() {
expect(fileHelpers.coverageFilePatterns()).toContain("jacoco*.xml");
});
});
describe("getFilePath()", () => {
it("should return path when file path has no starting slash", () => {
expect(fileHelpers.getFilePath("/usr/", "coverage.xml")).toEqual("/usr/coverage.xml")
})
it("should return path when file path has no starting slash", () => {
expect(fileHelpers.getFilePath("/usr", "coverage.xml")).toEqual("/usr/coverage.xml")
})
it("should return path when file path starts with a ./", () => {
expect(fileHelpers.getFilePath("/usr/", "./coverage.xml")).toEqual("./coverage.xml")
})
it("should return path when project root is . and filepath does not start with ./ or /", () => {
expect(fileHelpers.getFilePath(".", "coverage.xml")).toEqual("coverage.xml")
})
it("should return path when project root is . and filepath starts /", () => {
expect(fileHelpers.getFilePath(".", "/usr/coverage.xml")).toEqual("/usr/coverage.xml")
})
})
});
});

0 comments on commit 52803c7

Please sign in to comment.