Commit 6bd245cb authored by Scott Hampton's avatar Scott Hampton

Refactor file path formatting

Moved the `replace` file function to a util so that
it can be used in spec files.

Also wrote some specs to test that new util function.
parent 2c782b64
import { addIconStatus, formattedTime } from './utils';
import { addIconStatus, formatFilePath, formattedTime } from './utils';
export const getTestSuites = (state) => {
const { test_suites: testSuites = [] } = state.testReports;
......@@ -20,13 +20,7 @@ export const getSuiteTests = (state) => {
return testCases
.map((testCase) => ({
...testCase,
/**
* filePath is the file string appended onto the blob path.
* We need to make sure the file string doesn't start with `./` when appending.
* Even though we could leave the `/` at the beginning, we can't guarantee that the
* file string will have `/` at the beginning so we should just remove it and add it manually
*/
filePath: testCase.file ? `${state.blobPath}/${testCase.file.replace(/^\.?\//, '')}` : null,
filePath: testCase.file ? `${state.blobPath}/${formatFilePath(testCase.file)}` : null,
}))
.map(addIconStatus)
.slice(start, start + perPage);
......
import { __, sprintf } from '../../../locale';
import { TestStatus } from '../../constants';
/**
* Removes `./` from the beginning of a file path so it can be appended onto a blob path
* @param {String} file
* @returns {String} - formatted value
*/
export function formatFilePath(file) {
return file.replace(/^\.?\/*/, '');
}
export function iconForTestStatus(status) {
switch (status) {
case TestStatus.SUCCESS:
......
import { getJSONFixture } from 'helpers/fixtures';
import * as getters from '~/pipelines/stores/test_reports/getters';
import { iconForTestStatus, formattedTime } from '~/pipelines/stores/test_reports/utils';
import {
iconForTestStatus,
formatFilePath,
formattedTime,
} from '~/pipelines/stores/test_reports/utils';
describe('Getters TestReports Store', () => {
let state;
......@@ -76,7 +80,7 @@ describe('Getters TestReports Store', () => {
const expected = testReports.test_suites[0].test_cases
.map((x) => ({
...x,
filePath: `${state.blobPath}/${x.file.replace(/^\.?\//, '')}`,
filePath: `${state.blobPath}/${formatFilePath(x.file)}`,
formattedTime: formattedTime(x.execution_time),
icon: iconForTestStatus(x.status),
}))
......
import { formattedTime } from '~/pipelines/stores/test_reports/utils';
import { formatFilePath, formattedTime } from '~/pipelines/stores/test_reports/utils';
describe('Test reports utils', () => {
describe('formatFilePath', () => {
describe('when file string starts with "./"', () => {
it('should return the file string without the beginning "./"', () => {
const result = formatFilePath('./test.js');
expect(result).toBe('test.js');
});
});
describe('when file string starts with "/"', () => {
it('should return the file string without the beginning "/"', () => {
const result = formatFilePath('/test.js');
expect(result).toBe('test.js');
});
});
describe('when file string starts with more than one "/"', () => {
it('should return the file string without any of the beginning "/"', () => {
const result = formatFilePath('.//////////////test.js');
expect(result).toBe('test.js');
});
});
describe('when file string starts without either "." or "/"', () => {
it('should return the file string without change', () => {
const result = formatFilePath('test.js');
expect(result).toBe('test.js');
});
});
describe('when file string contains but does not start with "./"', () => {
it('should return the file string without change', () => {
const result = formatFilePath('mock/path./test.js');
expect(result).toBe('mock/path./test.js');
});
});
});
describe('formattedTime', () => {
describe('when time is smaller than a second', () => {
it('should return time in milliseconds fixed to 2 decimals', () => {
......
......@@ -4,6 +4,7 @@ import { GlButton, GlFriendlyWrap, GlLink, GlPagination } from '@gitlab/ui';
import { getJSONFixture } from 'helpers/fixtures';
import SuiteTable from '~/pipelines/components/test_reports/test_suite_table.vue';
import * as getters from '~/pipelines/stores/test_reports/getters';
import { formatFilePath } from '~/pipelines/stores/test_reports/utils';
import { TestStatus } from '~/pipelines/constants';
import skippedTestCases from './mock_data';
......@@ -85,8 +86,7 @@ describe('Test reports suite table', () => {
it('renders the file name for the test with a copy button', () => {
const { file } = testCases[0];
// remove `./` from the beginning of the file path
const relativeFile = file.replace(/^\.?\//, '');
const relativeFile = formatFilePath(file);
const filePath = `${blobPath}/${relativeFile}`;
const row = findCaseRowAtIndex(0);
const fileLink = findLinkForRow(row);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment