Merge pull request #43 from dorny/dotnet-trx

This commit is contained in:
Michal Dorner 2021-01-14 22:35:53 +01:00 committed by GitHub
commit dfddea6f3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 492 additions and 16 deletions

View file

@ -0,0 +1,53 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`dotnet-trx tests matches report snapshot 1`] = `
Object {
"annotations": Array [
Object {
"annotation_level": "failure",
"end_line": 9,
"message": "System.DivideByZeroException : Attempted to divide by zero.",
"path": "DotnetTests.Unit/Calculator.cs",
"start_line": 9,
"title": "[DotnetTests.XUnitTests.CalculatorTests] Exception_In_TargetTest",
},
Object {
"annotation_level": "failure",
"end_line": 39,
"message": "System.Exception : Test",
"path": "DotnetTests.XUnitTests/CalculatorTests.cs",
"start_line": 39,
"title": "[DotnetTests.XUnitTests.CalculatorTests] Exception_In_Test",
},
Object {
"annotation_level": "failure",
"end_line": 27,
"message": "Assert.Equal() Failure
Expected: 3
Actual: 2",
"path": "DotnetTests.XUnitTests/CalculatorTests.cs",
"start_line": 27,
"title": "[DotnetTests.XUnitTests.CalculatorTests] Failing_Test",
},
],
"summary": "**7** tests were completed in **1.061s** with **3** passed, **1** skipped and **3** failed.
| Result | Suite | Tests | Time | Passed ✔️ | Failed ❌ | Skipped ✖️ |
| :---: | :--- | ---: | ---: | ---: | ---: | ---: |
| ❌ | [DotnetTests.XUnitTests.CalculatorTests](#ts-0-DotnetTests-XUnitTests-CalculatorTests) | 7 | 109.5761ms | 3 | 3 | 1 |
# Test Suites
## <a id=\\"user-content-ts-0-DotnetTests-XUnitTests-CalculatorTests\\" href=\\"#ts-0-DotnetTests-XUnitTests-CalculatorTests\\">DotnetTests.XUnitTests.CalculatorTests</a> ❌
| Result | Test | Time |
| :---: | :--- | ---: |
| ❌ | Exception_In_TargetTest | 0.4975ms |
| ❌ | Exception_In_Test | 2.2728ms |
| ❌ | Failing_Test | 3.2953ms |
| ✔️ | Passing_Test | 0.1254ms |
| ✔️ | Passing_Test_With_Name | 0.103ms |
| ✖️ | Skipped_Test | 1ms |
| ✔️ | Timeout_Test | 102.2821ms |
",
"title": "Dotnet TRX tests ❌",
}
`;

View file

@ -0,0 +1,26 @@
import * as fs from 'fs'
import * as path from 'path'
import {parseDotnetTrx} from '../src/parsers/dotnet-trx/dotnet-trx-parser'
import {ParseOptions} from '../src/parsers/parser-types'
const xmlFixture = fs.readFileSync(path.join(__dirname, 'fixtures', 'dotnet-trx.trx'), {encoding: 'utf8'})
const outputPath = __dirname + '/__outputs__/dotnet-trx.md'
describe('dotnet-trx tests', () => {
it('matches report snapshot', async () => {
const opts: ParseOptions = {
name: 'Dotnet TRX tests',
annotations: true,
trackedFiles: ['DotnetTests.Unit/Calculator.cs', 'DotnetTests.XUnitTests/CalculatorTests.cs'],
workDir: 'C:/Users/Michal/Workspace/dorny/test-check/reports/dotnet/'
}
const result = await parseDotnetTrx(xmlFixture, opts)
fs.mkdirSync(path.dirname(outputPath), {recursive: true})
fs.writeFileSync(outputPath, result?.output?.summary ?? '')
expect(result.success).toBeFalsy()
expect(result?.output).toMatchSnapshot()
})
})

172
dist/index.js generated vendored
View file

@ -29,8 +29,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__webpack_require__(2186)); const core = __importStar(__webpack_require__(2186));
const github = __importStar(__webpack_require__(5438)); const github = __importStar(__webpack_require__(5438));
const jest_junit_parser_1 = __webpack_require__(1113);
const dart_json_parser_1 = __webpack_require__(4528); const dart_json_parser_1 = __webpack_require__(4528);
const dotnet_trx_parser_1 = __webpack_require__(2664);
const jest_junit_parser_1 = __webpack_require__(1113);
const file_utils_1 = __webpack_require__(2711); const file_utils_1 = __webpack_require__(2711);
const git_1 = __webpack_require__(9844); const git_1 = __webpack_require__(9844);
const github_utils_1 = __webpack_require__(3522); const github_utils_1 = __webpack_require__(3522);
@ -53,7 +54,7 @@ async function main() {
if (workDirInput) { if (workDirInput) {
process.chdir(workDirInput); process.chdir(workDirInput);
} }
const workDir = file_utils_1.normalizeDirPath(workDirInput || process.cwd(), true); const workDir = file_utils_1.normalizeDirPath(process.cwd(), true);
const octokit = github.getOctokit(token); const octokit = github.getOctokit(token);
const sha = github_utils_1.getCheckRunSha(); const sha = github_utils_1.getCheckRunSha();
// We won't need tracked files if we are not going to create annotations // We won't need tracked files if we are not going to create annotations
@ -86,7 +87,7 @@ function getParser(reporter) {
case 'dart-json': case 'dart-json':
return dart_json_parser_1.parseDartJson; return dart_json_parser_1.parseDartJson;
case 'dotnet-trx': case 'dotnet-trx':
throw new Error('Not implemented yet!'); return dotnet_trx_parser_1.parseDotnetTrx;
case 'flutter-machine': case 'flutter-machine':
return dart_json_parser_1.parseDartJson; return dart_json_parser_1.parseDartJson;
case 'jest-junit': case 'jest-junit':
@ -253,7 +254,7 @@ function getAnnotation(test, testSuite, workDir, trackedFiles) {
start_line: src.line, start_line: src.line,
end_line: src.line, end_line: src.line,
path: src.file, path: src.file,
message: `${(_e = test.error) === null || _e === void 0 ? void 0 : _e.error}\n\n${(_f = test.error) === null || _f === void 0 ? void 0 : _f.stackTrace}`, message: `${markdown_utils_1.fixEol((_e = test.error) === null || _e === void 0 ? void 0 : _e.error)}\n\n${markdown_utils_1.fixEol((_f = test.error) === null || _f === void 0 ? void 0 : _f.stackTrace)}`,
title: `[${testSuite.suite.path}] ${test.testStart.test.name}` title: `[${testSuite.suite.path}] ${test.testStart.test.name}`
}; };
} }
@ -324,6 +325,149 @@ function isDoneEvent(event) {
exports.isDoneEvent = isDoneEvent; exports.isDoneEvent = isDoneEvent;
/***/ }),
/***/ 2664:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.exceptionThrowSource = exports.parseDotnetTrx = void 0;
const xml2js_1 = __webpack_require__(6189);
const file_utils_1 = __webpack_require__(2711);
const xml_utils_1 = __webpack_require__(8653);
const markdown_utils_1 = __webpack_require__(6482);
const test_results_1 = __webpack_require__(8407);
const get_report_1 = __importDefault(__webpack_require__(3737));
class TestClass {
constructor(name) {
this.name = name;
this.tests = [];
}
}
class Test {
constructor(name, outcome, duration, error) {
this.name = name;
this.outcome = outcome;
this.duration = duration;
this.error = error;
}
get result() {
switch (this.outcome) {
case 'Passed':
return 'success';
case 'NotExecuted':
return 'skipped';
case 'Failed':
return 'failed';
}
}
}
async function parseDotnetTrx(content, options) {
const trx = (await xml2js_1.parseStringPromise(content, {
attrValueProcessors: [xml_utils_1.parseAttribute]
}));
const testClasses = getTestClasses(trx);
const testRun = getTestRunResult(trx, testClasses);
const success = testRun.result === 'success';
const icon = success ? markdown_utils_1.Icon.success : markdown_utils_1.Icon.fail;
return {
success,
output: {
title: `${options.name.trim()} ${icon}`,
summary: get_report_1.default(testRun),
annotations: options.annotations ? getAnnotations(testClasses, options.workDir, options.trackedFiles) : undefined
}
};
}
exports.parseDotnetTrx = parseDotnetTrx;
function getTestRunResult(trx, testClasses) {
const times = trx.TestRun.Times[0].$;
const totalTime = times.finish.getTime() - times.start.getTime();
const suites = testClasses.map(tc => {
const tests = tc.tests.map(t => new test_results_1.TestCaseResult(t.name, t.result, t.duration));
const group = new test_results_1.TestGroupResult(null, tests);
return new test_results_1.TestSuiteResult(tc.name, [group]);
});
return new test_results_1.TestRunResult(suites, totalTime);
}
function getTestClasses(trx) {
var _a;
const unitTests = {};
for (const td of trx.TestRun.TestDefinitions) {
for (const ut of td.UnitTest) {
unitTests[ut.$.id] = ut.TestMethod[0];
}
}
const unitTestsResults = trx.TestRun.Results.flatMap(r => r.UnitTestResult).flatMap(unitTestResult => ({
unitTestResult,
testMethod: unitTests[unitTestResult.$.testId]
}));
const testClasses = {};
for (const r of unitTestsResults) {
let tc = testClasses[r.testMethod.$.className];
if (tc === undefined) {
tc = new TestClass(r.testMethod.$.className);
testClasses[tc.name] = tc;
}
const output = r.unitTestResult.Output;
const error = (output === null || output === void 0 ? void 0 : output.length) > 0 && ((_a = output[0].ErrorInfo) === null || _a === void 0 ? void 0 : _a.length) > 0 ? output[0].ErrorInfo[0] : undefined;
const test = new Test(r.testMethod.$.name, r.unitTestResult.$.outcome, r.unitTestResult.$.duration, error);
tc.tests.push(test);
}
const result = Object.values(testClasses);
result.sort((a, b) => a.name.localeCompare(b.name));
for (const tc of result) {
tc.tests.sort((a, b) => a.name.localeCompare(b.name));
}
return result;
}
function getAnnotations(testClasses, workDir, trackedFiles) {
const annotations = [];
for (const tc of testClasses) {
for (const t of tc.tests) {
if (t.error) {
const src = exceptionThrowSource(t.error.StackTrace[0], workDir, trackedFiles);
if (src === null) {
continue;
}
annotations.push({
annotation_level: 'failure',
start_line: src.line,
end_line: src.line,
path: src.file,
message: markdown_utils_1.fixEol(t.error.Message[0]),
title: `[${tc.name}] ${t.name}`
});
}
}
}
return annotations;
}
function exceptionThrowSource(ex, workDir, trackedFiles) {
const lines = ex.split(/\r*\n/);
const re = / in (.+):line (\d+)$/;
for (const str of lines) {
const match = str.match(re);
if (match !== null) {
const [_, fileStr, lineStr] = match;
const filePath = file_utils_1.normalizeFilePath(fileStr);
const file = filePath.startsWith(workDir) ? filePath.substr(workDir.length) : filePath;
if (trackedFiles.includes(file)) {
const line = parseInt(lineStr);
return { file, line };
}
}
}
return null;
}
exports.exceptionThrowSource = exceptionThrowSource;
/***/ }), /***/ }),
/***/ 1113: /***/ 1113:
@ -415,7 +559,7 @@ function getAnnotations(junit, workDir, trackedFiles) {
start_line: src.line, start_line: src.line,
end_line: src.line, end_line: src.line,
path: src.file, path: src.file,
message: ex, message: markdown_utils_1.fixEol(ex),
title: `[${suite.$.name}] ${tc.$.name.trim()}` title: `[${suite.$.name}] ${tc.$.name.trim()}`
}); });
} }
@ -787,7 +931,7 @@ exports.getCheckRunSha = getCheckRunSha;
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.tableEscape = exports.table = exports.link = exports.details = exports.Icon = exports.Align = void 0; exports.fixEol = exports.tableEscape = exports.table = exports.link = exports.details = exports.Icon = exports.Align = void 0;
var Align; var Align;
(function (Align) { (function (Align) {
Align["Left"] = ":---"; Align["Left"] = ":---";
@ -819,6 +963,11 @@ function tableEscape(content) {
return content.toString().replace('|', '\\|'); return content.toString().replace('|', '\\|');
} }
exports.tableEscape = tableEscape; exports.tableEscape = tableEscape;
function fixEol(text) {
var _a;
return (_a = text === null || text === void 0 ? void 0 : text.replace(/\r/g, '')) !== null && _a !== void 0 ? _a : '';
}
exports.fixEol = fixEol;
/***/ }), /***/ }),
@ -855,10 +1004,21 @@ exports.slug = slug;
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.parseAttribute = void 0; exports.parseAttribute = void 0;
const isoDateRe = /^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)$/;
// matches dotnet duration: 00:00:00.0010000
const durationRe = /^(\d\d):(\d\d):(\d\d\.\d+)$/;
function parseAttribute(str) { function parseAttribute(str) {
if (str === '' || str === undefined) { if (str === '' || str === undefined) {
return str; return str;
} }
if (isoDateRe.test(str)) {
return new Date(str);
}
const durationMatch = str.match(durationRe);
if (durationMatch !== null) {
const [_, hourStr, minStr, secStr] = durationMatch;
return (parseInt(hourStr) * 3600 + parseInt(minStr) * 60 + parseFloat(secStr)) * 1000;
}
const num = parseFloat(str); const num = parseFloat(str);
if (isNaN(num)) { if (isNaN(num)) {
return str; return str;

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View file

@ -1,7 +1,8 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import * as github from '@actions/github' import * as github from '@actions/github'
import {parseJestJunit} from './parsers/jest-junit/jest-junit-parser'
import {parseDartJson} from './parsers/dart-json/dart-json-parser' import {parseDartJson} from './parsers/dart-json/dart-json-parser'
import {parseDotnetTrx} from './parsers/dotnet-trx/dotnet-trx-parser'
import {parseJestJunit} from './parsers/jest-junit/jest-junit-parser'
import {ParseOptions, ParseTestResult} from './parsers/parser-types' import {ParseOptions, ParseTestResult} from './parsers/parser-types'
import {getFileContent, normalizeDirPath} from './utils/file-utils' import {getFileContent, normalizeDirPath} from './utils/file-utils'
import {listFiles} from './utils/git' import {listFiles} from './utils/git'
@ -28,7 +29,7 @@ async function main(): Promise<void> {
process.chdir(workDirInput) process.chdir(workDirInput)
} }
const workDir = normalizeDirPath(workDirInput || process.cwd(), true) const workDir = normalizeDirPath(process.cwd(), true)
const octokit = github.getOctokit(token) const octokit = github.getOctokit(token)
const sha = getCheckRunSha() const sha = getCheckRunSha()
@ -67,7 +68,7 @@ function getParser(reporter: string): ParseTestResult {
case 'dart-json': case 'dart-json':
return parseDartJson return parseDartJson
case 'dotnet-trx': case 'dotnet-trx':
throw new Error('Not implemented yet!') return parseDotnetTrx
case 'flutter-machine': case 'flutter-machine':
return parseDartJson return parseDartJson
case 'jest-junit': case 'jest-junit':

View file

@ -2,7 +2,7 @@ import {Annotation, ParseOptions, TestResult} from '../parser-types'
import getReport from '../../report/get-report' import getReport from '../../report/get-report'
import {normalizeFilePath} from '../../utils/file-utils' import {normalizeFilePath} from '../../utils/file-utils'
import {Icon} from '../../utils/markdown-utils' import {Icon, fixEol} from '../../utils/markdown-utils'
import { import {
ReportEvent, ReportEvent,
@ -176,7 +176,7 @@ function getAnnotation(
start_line: src.line, start_line: src.line,
end_line: src.line, end_line: src.line,
path: src.file, path: src.file,
message: `${test.error?.error}\n\n${test.error?.stackTrace}`, message: `${fixEol(test.error?.error)}\n\n${fixEol(test.error?.stackTrace)}`,
title: `[${testSuite.suite.path}] ${test.testStart.test.name}` title: `[${testSuite.suite.path}] ${test.testStart.test.name}`
} }
} }

View file

@ -0,0 +1,157 @@
import {ErrorInfo, Outcome, TestMethod, TrxReport} from './dotnet-trx-types'
import {Annotation, ParseOptions, TestResult} from '../parser-types'
import {parseStringPromise} from 'xml2js'
import {normalizeFilePath} from '../../utils/file-utils'
import {parseAttribute} from '../../utils/xml-utils'
import {Icon, fixEol} from '../../utils/markdown-utils'
import {
TestExecutionResult,
TestRunResult,
TestSuiteResult,
TestGroupResult,
TestCaseResult
} from '../../report/test-results'
import getReport from '../../report/get-report'
class TestClass {
constructor(readonly name: string) {}
readonly tests: Test[] = []
}
class Test {
constructor(
readonly name: string,
readonly outcome: Outcome,
readonly duration: number,
readonly error?: ErrorInfo
) {}
get result(): TestExecutionResult {
switch (this.outcome) {
case 'Passed':
return 'success'
case 'NotExecuted':
return 'skipped'
case 'Failed':
return 'failed'
}
}
}
export async function parseDotnetTrx(content: string, options: ParseOptions): Promise<TestResult> {
const trx = (await parseStringPromise(content, {
attrValueProcessors: [parseAttribute]
})) as TrxReport
const testClasses = getTestClasses(trx)
const testRun = getTestRunResult(trx, testClasses)
const success = testRun.result === 'success'
const icon = success ? Icon.success : Icon.fail
return {
success,
output: {
title: `${options.name.trim()} ${icon}`,
summary: getReport(testRun),
annotations: options.annotations ? getAnnotations(testClasses, options.workDir, options.trackedFiles) : undefined
}
}
}
function getTestRunResult(trx: TrxReport, testClasses: TestClass[]): TestRunResult {
const times = trx.TestRun.Times[0].$
const totalTime = times.finish.getTime() - times.start.getTime()
const suites = testClasses.map(tc => {
const tests = tc.tests.map(t => new TestCaseResult(t.name, t.result, t.duration))
const group = new TestGroupResult(null, tests)
return new TestSuiteResult(tc.name, [group])
})
return new TestRunResult(suites, totalTime)
}
function getTestClasses(trx: TrxReport): TestClass[] {
const unitTests: {[id: string]: TestMethod} = {}
for (const td of trx.TestRun.TestDefinitions) {
for (const ut of td.UnitTest) {
unitTests[ut.$.id] = ut.TestMethod[0]
}
}
const unitTestsResults = trx.TestRun.Results.flatMap(r => r.UnitTestResult).flatMap(unitTestResult => ({
unitTestResult,
testMethod: unitTests[unitTestResult.$.testId]
}))
const testClasses: {[name: string]: TestClass} = {}
for (const r of unitTestsResults) {
let tc = testClasses[r.testMethod.$.className]
if (tc === undefined) {
tc = new TestClass(r.testMethod.$.className)
testClasses[tc.name] = tc
}
const output = r.unitTestResult.Output
const error = output?.length > 0 && output[0].ErrorInfo?.length > 0 ? output[0].ErrorInfo[0] : undefined
const test = new Test(r.testMethod.$.name, r.unitTestResult.$.outcome, r.unitTestResult.$.duration, error)
tc.tests.push(test)
}
const result = Object.values(testClasses)
result.sort((a, b) => a.name.localeCompare(b.name))
for (const tc of result) {
tc.tests.sort((a, b) => a.name.localeCompare(b.name))
}
return result
}
function getAnnotations(testClasses: TestClass[], workDir: string, trackedFiles: string[]): Annotation[] {
const annotations: Annotation[] = []
for (const tc of testClasses) {
for (const t of tc.tests) {
if (t.error) {
const src = exceptionThrowSource(t.error.StackTrace[0], workDir, trackedFiles)
if (src === null) {
continue
}
annotations.push({
annotation_level: 'failure',
start_line: src.line,
end_line: src.line,
path: src.file,
message: fixEol(t.error.Message[0]),
title: `[${tc.name}] ${t.name}`
})
}
}
}
return annotations
}
export function exceptionThrowSource(
ex: string,
workDir: string,
trackedFiles: string[]
): {file: string; line: number} | null {
const lines = ex.split(/\r*\n/)
const re = / in (.+):line (\d+)$/
for (const str of lines) {
const match = str.match(re)
if (match !== null) {
const [_, fileStr, lineStr] = match
const filePath = normalizeFilePath(fileStr)
const file = filePath.startsWith(workDir) ? filePath.substr(workDir.length) : filePath
if (trackedFiles.includes(file)) {
const line = parseInt(lineStr)
return {file, line}
}
}
}
return null
}

View file

@ -0,0 +1,60 @@
export interface TrxReport {
TestRun: TestRun
}
export interface TestRun {
Times: Times[]
Results: Results[]
TestDefinitions: TestDefinitions[]
}
export interface Times {
$: {
creation: Date
queuing: Date
start: Date
finish: Date
}
}
export interface TestDefinitions {
UnitTest: UnitTest[]
}
export interface UnitTest {
$: {
id: string
}
TestMethod: TestMethod[]
}
export interface TestMethod {
$: {
className: string
name: string
}
}
export interface Results {
UnitTestResult: UnitTestResult[]
}
export interface UnitTestResult {
$: {
testId: string
testName: string
duration: number
outcome: Outcome
}
Output: Output[]
}
export interface Output {
ErrorInfo: ErrorInfo[]
}
export interface ErrorInfo {
Message: string[]
StackTrace: string[]
}
export type Outcome = 'Passed' | 'NotExecuted' | 'Failed'

View file

@ -2,7 +2,7 @@ import {Annotation, ParseOptions, TestResult} from '../parser-types'
import {parseStringPromise} from 'xml2js' import {parseStringPromise} from 'xml2js'
import {JunitReport, TestCase, TestSuite} from './jest-junit-types' import {JunitReport, TestCase, TestSuite} from './jest-junit-types'
import {Icon} from '../../utils/markdown-utils' import {fixEol, Icon} from '../../utils/markdown-utils'
import {normalizeFilePath} from '../../utils/file-utils' import {normalizeFilePath} from '../../utils/file-utils'
import {parseAttribute} from '../../utils/xml-utils' import {parseAttribute} from '../../utils/xml-utils'
@ -91,7 +91,7 @@ function getAnnotations(junit: JunitReport, workDir: string, trackedFiles: strin
start_line: src.line, start_line: src.line,
end_line: src.line, end_line: src.line,
path: src.file, path: src.file,
message: ex, message: fixEol(ex),
title: `[${suite.$.name}] ${tc.$.name.trim()}` title: `[${suite.$.name}] ${tc.$.name.trim()}`
}) })
} }

View file

@ -50,7 +50,7 @@ export class TestSuiteResult {
} }
export class TestGroupResult { export class TestGroupResult {
constructor(readonly name: string | undefined, readonly tests: TestCaseResult[]) {} constructor(readonly name: string | undefined | null, readonly tests: TestCaseResult[]) {}
get passed(): number { get passed(): number {
return this.tests.reduce((sum, t) => (t.result === 'success' ? sum + 1 : sum), 0) return this.tests.reduce((sum, t) => (t.result === 'success' ? sum + 1 : sum), 0)

View file

@ -30,3 +30,7 @@ export function table(headers: ToString[], align: ToString[], ...rows: ToString[
export function tableEscape(content: ToString): string { export function tableEscape(content: ToString): string {
return content.toString().replace('|', '\\|') return content.toString().replace('|', '\\|')
} }
export function fixEol(text?: string): string {
return text?.replace(/\r/g, '') ?? ''
}

View file

@ -1,8 +1,23 @@
export function parseAttribute(str: string | undefined): string | number | undefined { const isoDateRe = /^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)$/
// matches dotnet duration: 00:00:00.0010000
const durationRe = /^(\d\d):(\d\d):(\d\d\.\d+)$/
export function parseAttribute(str: string | undefined): string | Date | number | undefined {
if (str === '' || str === undefined) { if (str === '' || str === undefined) {
return str return str
} }
if (isoDateRe.test(str)) {
return new Date(str)
}
const durationMatch = str.match(durationRe)
if (durationMatch !== null) {
const [_, hourStr, minStr, secStr] = durationMatch
return (parseInt(hourStr) * 3600 + parseInt(minStr) * 60 + parseFloat(secStr)) * 1000
}
const num = parseFloat(str) const num = parseFloat(str)
if (isNaN(num)) { if (isNaN(num)) {
return str return str