Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

[new-formatter] TAP formatter #2325

Merged
merged 5 commits into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/_data/formatters.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
"sample": "\nmyFile.ts\n1:14 semicolon Missing semicolon",
"consumer": "human"
},
{
"formatterName": "tap",
"description": "Formats output as TAP stream.",
"descriptionDetails": "Provides error messages output in TAP13 format which can be consumed by any TAP formatter.",
"sample": "\nTAP version 13\n1..1\nnot ok 1 - Some error\n ---\n message: Variable has any type\n severity: error\n data:\n ruleName: no-any\n fileName: test-file.ts\n line: 10\n character: 10\n failureString: Some error\n rawLines: Some raw output\n ...",
"consumer": "machine"
},
{
"formatterName": "verbose",
"description": "The human-readable formatter which includes the rule name in messages.",
Expand Down
24 changes: 24 additions & 0 deletions docs/formatters/tap/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
formatterName: tap
description: Formats output as TAP stream.
descriptionDetails: Provides error messages output in TAP13 format which can be consumed by any TAP formatter.
sample: |-

TAP version 13
1..1
not ok 1 - Some error
---
message: Variable has any type
severity: error
data:
ruleName: no-any
fileName: test-file.ts
line: 10
character: 10
failureString: Some error
rawLines: Some raw output
...
consumer: machine
layout: formatter
title: 'Formatter: tap'
---
1 change: 1 addition & 0 deletions src/formatters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export { Formatter as VerboseFormatter } from "./verboseFormatter";
export { Formatter as StylishFormatter } from "./stylishFormatter";
export { Formatter as FileslistFormatter } from "./fileslistFormatter";
export { Formatter as CodeFrameFormatter } from "./codeFrameFormatter";
export { Formatter as TapFormatter } from "./tapFormatter";
89 changes: 89 additions & 0 deletions src/formatters/tapFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @license
* Copyright 2017 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { AbstractFormatter } from "../language/formatter/abstractFormatter";
import { IFormatterMetadata } from "../language/formatter/formatter";
import { RuleFailure } from "../language/rule/rule";

import * as Utils from "../utils";

export class Formatter extends AbstractFormatter {
/* tslint:disable:object-literal-sort-keys */
public static metadata: IFormatterMetadata = {
formatterName: "tap",
description: "Formats output as TAP stream.",
descriptionDetails: "Provides error messages output in TAP13 format which can be consumed by any TAP formatter.",
sample: Utils.dedent`
TAP version 13
1..1
not ok 1 - Some error
---
message: Variable has any type
severity: error
data:
ruleName: no-any
fileName: test-file.ts
line: 10
character: 10
failureString: Some error
rawLines: Some raw output
...`,
consumer: "machine",
};
/* tslint:enable:object-literal-sort-keys */

public format(failures: RuleFailure[]): string {
let output: string[] = ["TAP version 13"];

if (failures.length === 0) {
output = output.concat([
"1..0 # SKIP No failures",
]);
} else {
output = output.concat([`1..${failures.length}`]).concat(this.mapToMessages(failures));
}

return output.join("\n") + "\n";
}

private mapToMessages(failures: RuleFailure[]): string[] {
return failures.map((failure: RuleFailure, i: number) => {
const fileName = failure.getFileName();
const failureString = failure.getFailure();
const ruleName = failure.getRuleName();
const failureMessage = failure.getFailure();
const failureSeverity = failure.getRuleSeverity();
const failureRaw = failure.getRawLines();
const lineAndCharacter = failure.getStartPosition().getLineAndCharacter();

return Utils.dedent`
not ok ${String(i + 1)} - ${failureMessage}
---
message : ${failureMessage}
severity: ${failureSeverity}
data:
ruleName: ${ruleName}
fileName: ${fileName}
line: ${String(lineAndCharacter.line)}
character: ${String(lineAndCharacter.character)}
failureString: ${failureString}
rawLines: ${failureRaw}
...`;
});

}
}
1 change: 1 addition & 0 deletions test/files/formatters/tapFormatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var x = 123;
79 changes: 79 additions & 0 deletions test/formatters/tapFormatterTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as ts from "typescript";

import { IFormatter, TestUtils } from "../lint";
import { createFailure } from "./utils";

import * as Utils from "../../src/utils";

describe("TAP Formatter", () => {
const TEST_FILE = "formatters/tapFormatter.test.ts";
let sourceFile: ts.SourceFile;
let formatter: IFormatter;

before(() => {
const Formatter = TestUtils.getFormatter("tap");
sourceFile = TestUtils.getSourceFile(TEST_FILE);
formatter = new Formatter();
});

it("formats failures", () => {
const maxPosition = sourceFile.getFullWidth();

const failures = [
createFailure(sourceFile, 0, 1, "first failure", "first-name", undefined, "error"),
createFailure(sourceFile, 32, 36, "mid failure", "mid-name", undefined, "error"),
createFailure(sourceFile, maxPosition - 1, maxPosition, "last failure", "last-name", undefined, "error"),
];

const expectedResult =
getFailureString(1, "first-name", "error", TEST_FILE, 0, 0, "first failure") +
getFailureString(2, "mid-name", "error", TEST_FILE, 1, 19, "mid failure") +
getFailureString(3, "last-name", "error", TEST_FILE, 0, 12, "last failure");

const actualResult = formatter.format(failures);
assert.equal(actualResult, `TAP version 13\n1..${failures.length}\n` + expectedResult);
});

it("handles no failures", () => {
const result = formatter.format([]);
assert.equal(result, "TAP version 13\n1..0 # SKIP No failures\n");
});

function getFailureString(num: number,
ruleName: string,
severity: string,
file: string,
line: number,
character: number,
reason: string) {
return Utils.dedent`
not ok ${String(num)} - ${reason}
---
message : ${reason}
severity: ${severity}
data:
ruleName: ${ruleName}
fileName: ${file}
line: ${String(line)}
character: ${String(character)}
failureString: ${reason}
rawLines: var x = 123;\n
...`;
}
});