Skip to content

Commit e03d453

Browse files
committed
feat(utils): refactor coverage config line parser into own util
1 parent 54ac87c commit e03d453

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/utils.ts

+47
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createHash, timingSafeEqual } from 'crypto';
22
import { readFile, createWriteStream } from 'fs';
3+
import { platform } from 'os';
34
import { promisify } from 'util';
45
import { getInput } from '@actions/core';
56
import fetch from 'node-fetch';
@@ -180,3 +181,49 @@ export async function verifySignature(
180181
return false;
181182
}
182183
}
184+
185+
/**
186+
* Parses a given coverage config line that looks like this –
187+
*
188+
* ```
189+
* /Users/gp/projects/cc/*.lcov:lcov
190+
* ```
191+
*
192+
* or –
193+
*
194+
* ```
195+
* D:\Users\gp\projects\cc\*.lcov:lcov
196+
* ```
197+
*
198+
* into –
199+
*
200+
* ```json
201+
* { "format": "lcov", "pattern": "/Users/gp/projects/cc/*.lcov" }
202+
* ```
203+
*
204+
* or –
205+
*
206+
* ```json
207+
* { "format": "lcov", "pattern": "D:\Users\gp\projects\cc\*.lcov" }
208+
* ```
209+
* @param coverageConfigLine
210+
* @returns
211+
*/
212+
export function parsePathAndFormat(coverageConfigLine: string): {
213+
format: string;
214+
pattern: string;
215+
} {
216+
let lineParts = coverageConfigLine.split(':');
217+
// On Windows, if the glob received an absolute path, the path will
218+
// include the Drive letter and the path – for example, `C:\Users\gp\projects\cc\*.lcov:lcov`
219+
// which leads to 2 colons. So we handle this special case.
220+
if (
221+
platform() === 'win32' &&
222+
(coverageConfigLine.match(/:/g) || []).length > 1
223+
) {
224+
lineParts = [lineParts.slice(0, -1).join(':'), lineParts.slice(-1)[0]];
225+
}
226+
const format = lineParts.slice(-1)[0];
227+
const pattern = lineParts.slice(0, -1)[0];
228+
return { format, pattern };
229+
}

0 commit comments

Comments
 (0)