Skip to content

Commit

Permalink
feat(utils): refactor coverage config line parser into own util
Browse files Browse the repository at this point in the history
  • Loading branch information
paambaati committed May 6, 2023
1 parent 54ac87c commit e03d453
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createHash, timingSafeEqual } from 'crypto';
import { readFile, createWriteStream } from 'fs';
import { platform } from 'os';
import { promisify } from 'util';
import { getInput } from '@actions/core';
import fetch from 'node-fetch';
Expand Down Expand Up @@ -180,3 +181,49 @@ export async function verifySignature(
return false;
}
}

/**
* Parses a given coverage config line that looks like this –
*
* ```
* /Users/gp/projects/cc/*.lcov:lcov
* ```
*
* or –
*
* ```
* D:\Users\gp\projects\cc\*.lcov:lcov
* ```
*
* into –
*
* ```json
* { "format": "lcov", "pattern": "/Users/gp/projects/cc/*.lcov" }
* ```
*
* or –
*
* ```json
* { "format": "lcov", "pattern": "D:\Users\gp\projects\cc\*.lcov" }
* ```
* @param coverageConfigLine
* @returns
*/
export function parsePathAndFormat(coverageConfigLine: string): {
format: string;
pattern: string;
} {
let lineParts = coverageConfigLine.split(':');
// On Windows, if the glob received an absolute path, the path will
// include the Drive letter and the path – for example, `C:\Users\gp\projects\cc\*.lcov:lcov`
// which leads to 2 colons. So we handle this special case.
if (
platform() === 'win32' &&
(coverageConfigLine.match(/:/g) || []).length > 1
) {
lineParts = [lineParts.slice(0, -1).join(':'), lineParts.slice(-1)[0]];
}
const format = lineParts.slice(-1)[0];
const pattern = lineParts.slice(0, -1)[0];
return { format, pattern };
}

0 comments on commit e03d453

Please sign in to comment.