From fe1b8ea378f75dc224eeda3a1528d487d678515b Mon Sep 17 00:00:00 2001 From: Nehal Sharma <68962290+N-Shar-ma@users.noreply.github.com> Date: Mon, 4 Oct 2021 23:15:51 -0700 Subject: [PATCH 1/8] Fix FileExplorer Path Splitting Bug Signed-off-by: N-Shar-ma --- .../src/components/FileExplorer/FileExplorer.tsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx index 21052c0793..5cd25f37de 100644 --- a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx +++ b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx @@ -56,9 +56,12 @@ const buildFileStructure = (row: CoverageTableRow) => { (acc: FileStructureObject, cur: CoverageTableRow) => { let path = cur.filename; if (row.path) { - path = path?.split(`${row.path}/`)[1]; + if (path) { + path = '/' + path; + } + path = path?.split(`/${row.path}/`)[1]; } - const pathArray = path?.split('/'); + const pathArray = path?.split('/').filter(el => el!==''); if (!pathArray) { return acc; From cb87427c0ed2752e20646ab0c516d913fff488cb Mon Sep 17 00:00:00 2001 From: N-Shar-ma Date: Tue, 19 Oct 2021 00:07:35 +0530 Subject: [PATCH 2/8] Fixed call stack bug, added a test Signed-off-by: N-Shar-ma --- .../FileExplorer/FileExplorer.test.tsx | 65 +++++++++++++++++++ .../components/FileExplorer/FileExplorer.tsx | 55 +++++++++------- 2 files changed, 95 insertions(+), 25 deletions(-) create mode 100644 plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx diff --git a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx new file mode 100644 index 0000000000..2162c25139 --- /dev/null +++ b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx @@ -0,0 +1,65 @@ +import { groupByPath } from './FileExplorer'; + +const dummyFiles = [ + { + filename: 'dir1/file1', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + path: '', + }, + { + filename: 'dir1/file2', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + path: '', + }, + { + filename: 'dir2/file3', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + path: '' + } +]; + +const dummyDataGroupedByPath = { + dir1: [ + { + filename: 'dir1/file1', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + path: '' + }, + { + filename: 'dir1/file2', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + path: '' + } + ], + dir2: [ + { + filename: 'dir2/file3', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + path: '' + } + ] +}; + +describe('groupByPath function', () => { + it('should group files by their root directory,as per their filename', () => { + expect(groupByPath(dummyFiles)).toBe(dummyDataGroupedByPath); + }); +}); \ No newline at end of file diff --git a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx index 5cd25f37de..e5627ab2c7 100644 --- a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx +++ b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx @@ -51,36 +51,41 @@ type CoverageTableRow = { tableData?: { id: number }; }; +export const groupByPath = (files: CoverageTableRow[]) => { + const acc: FileStructureObject = {}; + files.forEach (file => { + const filename = file.filename; + if (!file.filename) return; + const pathArray = filename?.split('/').filter( + el => el !== '' + ); + if (!acc.hasOwnProperty(pathArray[0])) { + acc[pathArray[0]] = []; + } + acc[pathArray[0]].push(file); + }); + return acc; +}; + +const trimFileNamesAsYouGo = (files: CoverageTableRow[], pathGroup: string) => { + return files.map(file => { + return { + ...file, + filename: file.filename ? file.filename.substring( + file.filename?.indexOf(pathGroup) + pathGroup.length + 1, + ) : file.filename, + }; + }) +}; + const buildFileStructure = (row: CoverageTableRow) => { - const dataGroupedByPath: FileStructureObject = row.files.reduce( - (acc: FileStructureObject, cur: CoverageTableRow) => { - let path = cur.filename; - if (row.path) { - if (path) { - path = '/' + path; - } - path = path?.split(`/${row.path}/`)[1]; - } - const pathArray = path?.split('/').filter(el => el!==''); - - if (!pathArray) { - return acc; - } - if (!acc.hasOwnProperty(pathArray[0])) { - acc[pathArray[0]] = []; - } - acc[pathArray[0]].push(cur); - return acc; - }, - {}, - ); - + const dataGroupedByPath: FileStructureObject = groupByPath(row.files); row.files = Object.keys(dataGroupedByPath).map(pathGroup => { return buildFileStructure({ path: pathGroup, files: dataGroupedByPath.hasOwnProperty('files') - ? dataGroupedByPath.files - : dataGroupedByPath[pathGroup], + ? trimFileNamesAsYouGo(dataGroupedByPath.files, pathGroup) + : trimFileNamesAsYouGo(dataGroupedByPath[pathGroup], pathGroup), coverage: dataGroupedByPath[pathGroup].reduce( (acc: number, cur: CoverageTableRow) => acc + cur.coverage, From 6bfa59e323ca61db813130191a1ed247d5cd33d7 Mon Sep 17 00:00:00 2001 From: N-Shar-ma Date: Tue, 19 Oct 2021 02:13:37 +0530 Subject: [PATCH 3/8] Minor tsc check passing fixes Signed-off-by: N-Shar-ma --- .../components/FileExplorer/FileExplorer.tsx | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx index e5627ab2c7..bc36af265d 100644 --- a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx +++ b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx @@ -59,23 +59,27 @@ export const groupByPath = (files: CoverageTableRow[]) => { const pathArray = filename?.split('/').filter( el => el !== '' ); - if (!acc.hasOwnProperty(pathArray[0])) { - acc[pathArray[0]] = []; + if (pathArray) { + if (!acc.hasOwnProperty(pathArray[0])) { + acc[pathArray[0]] = []; + } + acc[pathArray[0]].push(file); } - acc[pathArray[0]].push(file); }); return acc; }; -const trimFileNamesAsYouGo = (files: CoverageTableRow[], pathGroup: string) => { +const removeVisitedPathGroup = (files: CoverageTableRow[], pathGroup: string) => { return files.map(file => { return { ...file, - filename: file.filename ? file.filename.substring( - file.filename?.indexOf(pathGroup) + pathGroup.length + 1, - ) : file.filename, + filename: file.filename + ? file.filename.substring( + file.filename?.indexOf(pathGroup) + pathGroup.length + 1, + ) + : file.filename, }; - }) + }); }; const buildFileStructure = (row: CoverageTableRow) => { @@ -84,8 +88,8 @@ const buildFileStructure = (row: CoverageTableRow) => { return buildFileStructure({ path: pathGroup, files: dataGroupedByPath.hasOwnProperty('files') - ? trimFileNamesAsYouGo(dataGroupedByPath.files, pathGroup) - : trimFileNamesAsYouGo(dataGroupedByPath[pathGroup], pathGroup), + ? removeVisitedPathGroup(dataGroupedByPath.files, pathGroup) + : removeVisitedPathGroup(dataGroupedByPath[pathGroup], pathGroup), coverage: dataGroupedByPath[pathGroup].reduce( (acc: number, cur: CoverageTableRow) => acc + cur.coverage, From f7a7fdc2afcf16541036b92e88d00d85d66406ba Mon Sep 17 00:00:00 2001 From: Jeremy Guarini Date: Thu, 21 Oct 2021 12:28:43 -0700 Subject: [PATCH 4/8] fixed prettier issues with FileExplorer files Signed-off-by: Jeremy Guarini --- .../FileExplorer/FileExplorer.test.tsx | 33 ++++++++++++++----- .../components/FileExplorer/FileExplorer.tsx | 13 ++++---- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx index 2162c25139..ff5845edfe 100644 --- a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx +++ b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx @@ -1,3 +1,18 @@ +/* + * Copyright 2021 The Backstage Authors + * + * 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 { groupByPath } from './FileExplorer'; const dummyFiles = [ @@ -23,8 +38,8 @@ const dummyFiles = [ coverage: 1, missing: 1, tracked: 1, - path: '' - } + path: '', + }, ]; const dummyDataGroupedByPath = { @@ -35,7 +50,7 @@ const dummyDataGroupedByPath = { coverage: 1, missing: 1, tracked: 1, - path: '' + path: '', }, { filename: 'dir1/file2', @@ -43,8 +58,8 @@ const dummyDataGroupedByPath = { coverage: 1, missing: 1, tracked: 1, - path: '' - } + path: '', + }, ], dir2: [ { @@ -53,13 +68,13 @@ const dummyDataGroupedByPath = { coverage: 1, missing: 1, tracked: 1, - path: '' - } - ] + path: '', + }, + ], }; describe('groupByPath function', () => { it('should group files by their root directory,as per their filename', () => { expect(groupByPath(dummyFiles)).toBe(dummyDataGroupedByPath); }); -}); \ No newline at end of file +}); diff --git a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx index bc36af265d..fbe74ff7e5 100644 --- a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx +++ b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx @@ -53,12 +53,10 @@ type CoverageTableRow = { export const groupByPath = (files: CoverageTableRow[]) => { const acc: FileStructureObject = {}; - files.forEach (file => { + files.forEach(file => { const filename = file.filename; if (!file.filename) return; - const pathArray = filename?.split('/').filter( - el => el !== '' - ); + const pathArray = filename?.split('/').filter(el => el !== ''); if (pathArray) { if (!acc.hasOwnProperty(pathArray[0])) { acc[pathArray[0]] = []; @@ -69,11 +67,14 @@ export const groupByPath = (files: CoverageTableRow[]) => { return acc; }; -const removeVisitedPathGroup = (files: CoverageTableRow[], pathGroup: string) => { +const removeVisitedPathGroup = ( + files: CoverageTableRow[], + pathGroup: string, +) => { return files.map(file => { return { ...file, - filename: file.filename + filename: file.filename ? file.filename.substring( file.filename?.indexOf(pathGroup) + pathGroup.length + 1, ) From b3fb448e7bd22db2da17be0eae1feab2c8d4026a Mon Sep 17 00:00:00 2001 From: Jeremy Guarini Date: Mon, 25 Oct 2021 08:53:56 -0700 Subject: [PATCH 5/8] removed extra line in imports Signed-off-by: Jeremy Guarini --- .../code-coverage/src/components/FileExplorer/FileExplorer.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx index fbe74ff7e5..ed31cb9c62 100644 --- a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx +++ b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx @@ -30,7 +30,6 @@ import { useAsync } from 'react-use'; import { codeCoverageApiRef } from '../../api'; import { FileEntry } from '../../types'; import { FileContent } from './FileContent'; - import { Progress, ResponseErrorPanel, From 7ea444c78d28af004a9c11bf2f8db0af9234c028 Mon Sep 17 00:00:00 2001 From: Jeremy Guarini Date: Tue, 26 Oct 2021 10:26:23 -0700 Subject: [PATCH 6/8] update filexplorer test Signed-off-by: Jeremy Guarini --- .../src/components/FileExplorer/FileExplorer.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx index ff5845edfe..948d9c9722 100644 --- a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx +++ b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx @@ -75,6 +75,6 @@ const dummyDataGroupedByPath = { describe('groupByPath function', () => { it('should group files by their root directory,as per their filename', () => { - expect(groupByPath(dummyFiles)).toBe(dummyDataGroupedByPath); + expect(groupByPath(dummyFiles)).toStrictEqual(dummyDataGroupedByPath); }); }); From 72e7a876840642ad8ae7295abcd86e46a537fe3a Mon Sep 17 00:00:00 2001 From: Jeremy Guarini Date: Wed, 27 Oct 2021 09:36:30 -0700 Subject: [PATCH 7/8] add more testing for FileExplorer tests Signed-off-by: Jeremy Guarini --- .../FileExplorer/FileExplorer.test.tsx | 144 +++++++++++++++++- .../components/FileExplorer/FileExplorer.tsx | 2 +- 2 files changed, 144 insertions(+), 2 deletions(-) diff --git a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx index 948d9c9722..4163b22ac1 100644 --- a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx +++ b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.test.tsx @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { groupByPath } from './FileExplorer'; +import { groupByPath, buildFileStructure } from './FileExplorer'; const dummyFiles = [ { @@ -40,6 +40,22 @@ const dummyFiles = [ tracked: 1, path: '', }, + { + filename: 'dir3/dir2/dir1/file4', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + path: '', + }, + { + filename: 'dir3/dir2/dir3/file4', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + path: '', + }, ]; const dummyDataGroupedByPath = { @@ -71,6 +87,124 @@ const dummyDataGroupedByPath = { path: '', }, ], + dir3: [ + { + filename: 'dir3/dir2/dir1/file4', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + path: '', + }, + { + filename: 'dir3/dir2/dir3/file4', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + path: '', + }, + ], +}; + +const coverageTableRow = { + files: dummyFiles, + coverage: 1, + missing: 1, + tracked: 1, + path: '', +}; + +const coverageTableRowResults = { + files: [ + { + path: 'dir1', + files: [ + { + path: 'file1', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + }, + { + path: 'file2', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + }, + ], + coverage: 1, + missing: 2, + tracked: 2, + }, + { + path: 'dir2', + files: [ + { + path: 'file3', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + }, + ], + coverage: 1, + missing: 1, + tracked: 1, + }, + { + path: 'dir3', + files: [ + { + path: 'dir2', + files: [ + { + path: 'dir1', + files: [ + { + path: 'file4', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + }, + ], + coverage: 1, + missing: 1, + tracked: 1, + }, + { + path: 'dir3', + files: [ + { + path: 'file4', + files: [], + coverage: 1, + missing: 1, + tracked: 1, + }, + ], + coverage: 1, + missing: 1, + tracked: 1, + }, + ], + coverage: 1, + missing: 2, + tracked: 2, + }, + ], + coverage: 1, + missing: 2, + tracked: 2, + }, + ], + coverage: 1, + missing: 1, + tracked: 1, + path: '', }; describe('groupByPath function', () => { @@ -78,3 +212,11 @@ describe('groupByPath function', () => { expect(groupByPath(dummyFiles)).toStrictEqual(dummyDataGroupedByPath); }); }); + +describe('buildFileStructure function', () => { + it('should group files by their root directory,as per their filename', () => { + expect(buildFileStructure(coverageTableRow)).toStrictEqual( + coverageTableRowResults, + ); + }); +}); diff --git a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx index ed31cb9c62..49379551ad 100644 --- a/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx +++ b/plugins/code-coverage/src/components/FileExplorer/FileExplorer.tsx @@ -82,7 +82,7 @@ const removeVisitedPathGroup = ( }); }; -const buildFileStructure = (row: CoverageTableRow) => { +export const buildFileStructure = (row: CoverageTableRow) => { const dataGroupedByPath: FileStructureObject = groupByPath(row.files); row.files = Object.keys(dataGroupedByPath).map(pathGroup => { return buildFileStructure({ From 767fe912b2be3ef2ec3fccfa7c68820b17de3bd1 Mon Sep 17 00:00:00 2001 From: Jeremy Guarini Date: Wed, 27 Oct 2021 09:47:30 -0700 Subject: [PATCH 8/8] add changeset Signed-off-by: Jeremy Guarini --- .changeset/weak-readers-know.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/weak-readers-know.md diff --git a/.changeset/weak-readers-know.md b/.changeset/weak-readers-know.md new file mode 100644 index 0000000000..24cf29a5db --- /dev/null +++ b/.changeset/weak-readers-know.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-code-coverage': patch +--- + +Addresses bug when file path contains multiple directories with the same name.