Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only count something as a directory if it has children #4556

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 0 additions & 2 deletions e2e/playwright/file-tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ test.describe('integrations tests', () => {
{
title: 'test-sample',
fileCount: 1,
folderCount: 1,
},
],
sortBy: 'last-modified-desc',
Expand Down Expand Up @@ -233,7 +232,6 @@ test.describe('when using the file tree to', () => {
{
title: projectName,
fileCount: 2,
folderCount: 2, // TODO: This is a pre-existing bug, there are no folders within the project
},
],
sortBy: 'last-modified-desc',
Expand Down
5 changes: 1 addition & 4 deletions e2e/playwright/fixtures/homePageFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { expect } from '@playwright/test'
interface ProjectCardState {
title: string
fileCount: number
folderCount: number
}

interface HomePageState {
Expand Down Expand Up @@ -61,15 +60,13 @@ export class HomePageFixture {
const projectCards = await this.projectCard.all()
const projectCardStates: Array<ProjectCardState> = []
for (const projectCard of projectCards) {
const [title, fileCount, folderCount] = await Promise.all([
const [title, fileCount] = await Promise.all([
(await projectCard.locator(this.projectCardTitle).textContent()) || '',
Number(await projectCard.locator(this.projectCardFile).textContent()),
Number(await projectCard.locator(this.projectCardFolder).textContent()),
])
projectCardStates.push({
title: title,
fileCount,
folderCount,
})
}
return projectCardStates
Expand Down
25 changes: 23 additions & 2 deletions src/lib/desktop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,21 @@ describe('desktop utilities', () => {
'project-without-kcl-files',
'another-valid-project',
],
'/test/projects/valid-project': ['file1.kcl', 'file2.stp'],
'/test/projects/valid-project': [
'file1.kcl',
'file2.stp',
'file3.kcl',
'directory1',
],
'/test/projects/valid-project/directory1': [],
'/test/projects/project-without-kcl-files': ['file3.glb'],
'/test/projects/another-valid-project': ['file4.kcl'],
'/test/projects/another-valid-project': [
'file4.kcl',
'directory2',
'directory3',
],
'/test/projects/another-valid-project/directory2': [],
'/test/projects/another-valid-project/directory3': [],
}

beforeEach(() => {
Expand Down Expand Up @@ -119,6 +131,15 @@ describe('desktop utilities', () => {
)
})

it('correctly counts directories and files', async () => {
const projects = await listProjects(mockConfig)
// Verify that directories and files are counted correctly
expect(projects[0].directory_count).toEqual(1)
expect(projects[0].kcl_file_count).toEqual(2)
expect(projects[1].directory_count).toEqual(2)
expect(projects[1].kcl_file_count).toEqual(1)
})

it('handles empty project directory', async () => {
// Adjust mockFileSystem to simulate empty directory
mockFileSystem['/test/projects'] = []
Expand Down
5 changes: 4 additions & 1 deletion src/lib/desktop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,10 @@ const directoryCount = (file: FileEntry) => {
let count = 0
if (file.children) {
for (let entry of file.children) {
count += 1
// We only want to count FileEntries with children, e.g. folders
if (entry.children !== null) {
count += 1
}
Comment on lines +311 to +313
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have done if (!entry.children) continue; but this works too :P

directoryCount(entry)
}
}
Expand Down