Skip to content

Commit

Permalink
fix: populate axe-core version based on input rather than hardcoding …
Browse files Browse the repository at this point in the history
…version 3.2.2 (#68)

* Adds `axeVersion` to `EnvironmentData`
* Populates it appropriately in `environment-data-provider`
* Uses it in `axe-tool-property-provider` in place of hardcoded version
* Updates converters to pass environment data to the provider
* Updates all tests accordingly
  • Loading branch information
dbjorge authored Jun 28, 2019
1 parent 59d65aa commit 38a15d4
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 36 deletions.
10 changes: 5 additions & 5 deletions src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14881,7 +14881,7 @@ Fix any of the following:
}
`;

exports[`public convertAxeToSarif API converts empty/minimal axe v2 input with no results to the pinned minimal SARIF output 1`] = `
exports[`public convertAxeToSarif API converts minimal axe v2 input with no results to the pinned minimal SARIF output 1`] = `
Object {
"runs": Array [
Object {
Expand Down Expand Up @@ -15560,15 +15560,15 @@ Object {
],
"tool": Object {
"driver": Object {
"downloadUri": "https://www.npmjs.com/package/axe-core/v/3.2.2",
"fullName": "axe for Web v3.2.2",
"downloadUri": "https://www.npmjs.com/package/axe-core/v/1.2.3",
"fullName": "axe for Web v1.2.3",
"informationUri": "https://www.deque.com/axe/axe-for-web/",
"name": "axe-core",
"properties": Object {
"microsoft/qualityDomain": "Accessibility",
},
"rules": Array [],
"semanticVersion": "3.2.2",
"semanticVersion": "1.2.3",
"shortDescription": Object {
"text": "An open source accessibility rules library for automated testing.",
},
Expand All @@ -15579,7 +15579,7 @@ Object {
"name": "WCAG",
},
],
"version": "3.2.2",
"version": "1.2.3",
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions src/artifact-property-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ describe('artifact-property-provider', () => {
const targetPageUrl: string = 'target_page_url_stub';
const targetPageTitle: string = 'target_page_title_stub';
const timestamp: string = 'timestamp_stub';
const axeVersion: string = 'axe_version_stub';

it('returns artifact object with the provided environment data', () => {
const environmentData: EnvironmentData = {
targetPageUrl: targetPageUrl,
targetPageTitle: targetPageTitle,
timestamp: timestamp,
axeVersion: axeVersion,
};

const expectedResults: Sarif.Artifact = {
Expand Down
7 changes: 4 additions & 3 deletions src/axe-raw-sarif-converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('AxeRawSarifConverter', () => {
const environmentDataStub: EnvironmentData = {
timestamp: axeResult.timestamp,
targetPageUrl: axeResult.url,
axeVersion: axeResult.testEngine.version,
};

const axeRawToSarifOutput = testSubject.convert(
Expand Down Expand Up @@ -127,10 +128,10 @@ describe('AxeRawSarifConverter', () => {

it('outputs a sarif log whose run uses the axeToolPropertyProvider to populate the tool property', () => {
const axeToolPropertyProviderMock: IMock<
() => Sarif.ToolComponent
(environmentData: EnvironmentData) => Sarif.ToolComponent
> = Mock.ofInstance(getAxeToolProperties);
axeToolPropertyProviderMock
.setup(ap => ap())
.setup(ap => ap(stubEnvironmentData))
.returns(() => stubToolProperties['driver'])
.verifiable(Times.once());

Expand Down Expand Up @@ -229,7 +230,7 @@ describe('AxeRawSarifConverter', () => {
(environmentData: EnvironmentData) => Sarif.Artifact
> = Mock.ofInstance(getArtifactProperties);
artifactPropertyProviderMock
.setup(ap => ap(It.isAny()))
.setup(ap => ap(stubEnvironmentData))
.returns(() => stubArtifactProperties)
.verifiable(Times.once());

Expand Down
6 changes: 4 additions & 2 deletions src/axe-raw-sarif-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export class AxeRawSarifConverter {
);
public constructor(
private getConverterToolProperties: () => Sarif.Run['conversion'],
private getAxeProperties: () => Sarif.ToolComponent,
private getAxeProperties: (
environmentData: EnvironmentData,
) => Sarif.ToolComponent,
private invocationConverter: (
environmentData: EnvironmentData,
) => Sarif.Invocation[],
Expand Down Expand Up @@ -71,7 +73,7 @@ export class AxeRawSarifConverter {
conversion: this.getConverterToolProperties(),
tool: {
driver: {
...this.getAxeProperties(),
...this.getAxeProperties(environmentData),
rules: resultToRuleConverter.getRulePropertiesFromResults(),
},
},
Expand Down
17 changes: 12 additions & 5 deletions src/axe-tool-property-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,36 @@
// Licensed under the MIT License.
import * as Sarif from 'sarif';
import { getAxeToolProperties } from './axe-tool-property-provider';
import { EnvironmentData } from './environment-data';
import { getWcagTaxonomyReference } from './wcag-taxonomy-provider';

describe('axe-tool-property-provider', () => {
describe('getAxeToolProperties', () => {
it('returns the axe properties as a Sarif.ToolComponent', () => {
const expectedResults: Sarif.ToolComponent = {
name: 'axe-core',
fullName: 'axe for Web v3.2.2',
fullName: 'axe for Web v1.2.3',
shortDescription: {
text:
'An open source accessibility rules library for automated testing.',
},
version: '3.2.2',
semanticVersion: '3.2.2',
version: '1.2.3',
semanticVersion: '1.2.3',
informationUri: 'https://www.deque.com/axe/axe-for-web/',
downloadUri: 'https://www.npmjs.com/package/axe-core/v/3.2.2',
downloadUri: 'https://www.npmjs.com/package/axe-core/v/1.2.3',
properties: {
'microsoft/qualityDomain': 'Accessibility',
},
supportedTaxonomies: [getWcagTaxonomyReference()],
};

const actualResults: Sarif.ToolComponent = getAxeToolProperties();
const stubEnvironmentData = {
axeVersion: '1.2.3',
} as EnvironmentData;

const actualResults: Sarif.ToolComponent = getAxeToolProperties(
stubEnvironmentData,
);
expect(actualResults).toEqual(expectedResults);
});
});
Expand Down
14 changes: 9 additions & 5 deletions src/axe-tool-property-provider.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as Sarif from 'sarif';
import { EnvironmentData } from './environment-data';
import { getWcagTaxonomyReference } from './wcag-taxonomy-provider';

export function getAxeToolProperties(): Sarif.ToolComponent {
export function getAxeToolProperties(
environmentData: EnvironmentData,
): Sarif.ToolComponent {
const version = environmentData.axeVersion;
return {
name: 'axe-core',
fullName: 'axe for Web v3.2.2',
fullName: `axe for Web v${version}`,
shortDescription: {
text:
'An open source accessibility rules library for automated testing.',
},
version: '3.2.2',
semanticVersion: '3.2.2',
version: version,
semanticVersion: version,
informationUri: 'https://www.deque.com/axe/axe-for-web/',
downloadUri: 'https://www.npmjs.com/package/axe-core/v/3.2.2',
downloadUri: `https://www.npmjs.com/package/axe-core/v/${version}`,
properties: {
'microsoft/qualityDomain': 'Accessibility',
},
Expand Down
31 changes: 29 additions & 2 deletions src/environment-data-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,53 @@ describe('environment-data-provider', () => {
it('returns an EnvironmentData object from environment data extracted from AxeResults', () => {
const stubTimestamp: string = 'stub_timestamp';
const stubTargetPageUrl: string = 'https://example.com';
const stubAxeVersion: string = 'stub_axe_version';

const stubAxeResults: Axe.AxeResults = {
url: stubTargetPageUrl,
timestamp: stubTimestamp,
testEngine: {
name: 'stub_axe_name',
version: stubAxeVersion,
},
passes: [],
incomplete: [],
violations: [],
inapplicable: [],
toolOptions: {} as Axe.RunOptions,
testEngine: {} as Axe.TestEngine,
testRunner: {} as Axe.TestRunner,
testEnvironment: {} as Axe.TestEnvironment,
};

const expectedResults: EnvironmentData = {
timestamp: stubTimestamp,
targetPageUrl: stubTargetPageUrl,
axeVersion: stubAxeVersion,
};

const actualResults = getEnvironmentDataFromResults(stubAxeResults);
expect(actualResults).toEqual(expectedResults);
});
});

describe('getEnvironmentDataFromEnvironment', () => {
describe('getEnvironmentDataFromEnvironment in axe-like environment', () => {
beforeAll(() => {
(global as any).axe = { version: 'stub_axe_version' };
});

afterAll(() => {
delete (global as any).axe;
});

it('returns an EnvironmentData object from environment data extracted from the environment', () => {
const actualResults = getEnvironmentDataFromEnvironment();
expect(actualResults).toHaveProperty('timestamp');
expect(actualResults).toHaveProperty('targetPageUrl');
expect(actualResults).toHaveProperty('targetPageTitle');
expect(actualResults).toHaveProperty(
'axeVersion',
'stub_axe_version',
);
});

it('contains a timestamp with a valid ISO format', () => {
Expand All @@ -57,4 +74,14 @@ describe('environment-data-provider', () => {
expect(actualTimestamp).toBe(expectedTimestamp);
});
});

describe('getEnvironmentDataFromEnvironment outside of axe-like environment', () => {
it('throws a descriptive error when it cannot infer axe version', () => {
expect(
getEnvironmentDataFromEnvironment,
).toThrowErrorMatchingInlineSnapshot(
`"Could not infer axe version from global axe object. Are you running from the context of an axe reporter?"`,
);
});
});
});
14 changes: 14 additions & 0 deletions src/environment-data-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@ export function getEnvironmentDataFromResults(
return {
timestamp: axeResults.timestamp,
targetPageUrl: axeResults.url,
axeVersion: axeResults.testEngine.version,
};
}

export function getEnvironmentDataFromEnvironment(): EnvironmentData {
// We use the global "axe" object to detect this rather than saying
// "import { version } from 'axe-core'"" because we want to pick up the
// version of axe that is invoking us, which will usually be a *peer*
// dependency, rather than using the version that axe-sarif-converter built
// against as a child dependency.
const globalAxeVersion = (global as any).axe && (global as any).axe.version;
if (!globalAxeVersion) {
throw Error(
'Could not infer axe version from global axe object. Are you running from the context of an axe reporter?',
);
}

return {
timestamp: new Date().toISOString(),
targetPageUrl: window.location.href,
targetPageTitle: document.title,
axeVersion: globalAxeVersion,
};
}
1 change: 1 addition & 0 deletions src/environment-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface EnvironmentData {
targetPageUrl: string;
targetPageTitle?: string;
timestamp: string;
axeVersion: string;
}
10 changes: 8 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ function readTestResourceJSON(testResourceFileName: string): any {
}

describe('public convertAxeToSarif API', () => {
it('converts empty/minimal axe v2 input with no results to the pinned minimal SARIF output', () => {
it('converts minimal axe v2 input with no results to the pinned minimal SARIF output', () => {
// "Minimal" means "just enough for the converter to infer required EnvironmentData"
const minimalAxeV2Input: AxeResults = {
toolOptions: {} as RunOptions,
testEngine: {} as TestEngine,
testEngine: {
version: '1.2.3',
} as TestEngine,
testRunner: {} as TestRunner,
testEnvironment: {} as TestEnvironment,
url: 'https://example.com',
Expand Down Expand Up @@ -63,6 +66,9 @@ describe('public convertAxeToSarif API', () => {
});
});

// This initializes global state that the reporter API assumes is available
require('axe-core');

describe('public sarifReporter API', () => {
const emptyAxeRunOptions = {};

Expand Down
1 change: 1 addition & 0 deletions src/invocation-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('invocation-provider', () => {
targetPageUrl: 'https://example.com',
targetPageTitle: 'Environment Data Stub',
timestamp: '2018-03-23T21:36:58.321Z',
axeVersion: 'stub_version',
};
const invocationStub: Sarif.Invocation[] = [
{
Expand Down
Loading

0 comments on commit 38a15d4

Please sign in to comment.