Skip to content

Comments

feat: add --prune-empty-dirs flag to remove empty directories regardless of age#314

Open
danstis wants to merge 3 commits intomainfrom
feature/296-prune-empty-dirs
Open

feat: add --prune-empty-dirs flag to remove empty directories regardless of age#314
danstis wants to merge 3 commits intomainfrom
feature/296-prune-empty-dirs

Conversation

@danstis
Copy link
Owner

@danstis danstis commented Jan 20, 2026

User description

Summary

This PR adds a new flag --prune-empty-dirs (default: false) that allows users to remove empty directories even if they are not considered stale.

Changes

  • Updated rmstale.go:
    • Added pruneEmptyDirs flag.
    • Updated procDir and handleEmptyDirectory signatures to propagate the flag.
    • Updated handleEmptyDirectory logic to remove directory if empty and pruneEmptyDirs is set, OR if it matches existing stale criteria.
  • Updated rmstale_test.go:
    • Updated all existing calls to procDir and handleEmptyDirectory to match new signatures.
    • Added TestPruneEmptyDirsOption to verify the new behavior.

Issue

Fixes #296


PR Type

Enhancement, Tests


Description

  • Add --prune-empty-dirs flag to remove empty directories

  • Update procDir and handleEmptyDirectory functions to support new flag

  • Implement tests for --prune-empty-dirs functionality


Diagram Walkthrough

flowchart LR
  A["Add pruneEmptyDirs flag"] --> B["Update procDir function"]
  A --> C["Update handleEmptyDirectory function"]
  B --> D["Modify function signatures"]
  C --> D
  D --> E["Implement TestPruneEmptyDirsOption"]
Loading

File Walkthrough

Relevant files
Enhancement
rmstale.go
Add and integrate `pruneEmptyDirs` flag in main logic       

rmstale.go

  • Add pruneEmptyDirs flag to command-line options
  • Update procDir function signature to include pruneEmptyDirs
  • Update handleEmptyDirectory logic to use pruneEmptyDirs
+11/-7   
Tests
rmstale_test.go
Add tests for `--prune-empty-dirs` functionality                 

rmstale_test.go

  • Update tests to include pruneEmptyDirs flag
  • Add TestPruneEmptyDirsOption to verify new behavior
+28/-6   

@sophie-syntax sophie-syntax bot changed the title feat: add --prune-empty-dirs flag feat: add --prune-empty-dirs flag to remove empty directories regardless of age Jan 20, 2026
@sophie-syntax
Copy link
Contributor

sophie-syntax bot commented Jan 20, 2026

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis ✅

296 - Fully compliant

Compliant requirements:

  • Introduce a new --prune-empty-dirs flag (default false).
  • Remove empty directories regardless of their age when the flag is enabled.
  • Ensure empty directory structures are cleaned up even if not stale.
  • Add tests to verify the new behavior.
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Ext filter interaction

When --prune-empty-dirs is set, the code ignores the extension filter (ext), removing empty directories even if a filter was applied. Confirm if this is intended behavior.

if empty {
	if pruneEmptyDirs || (isStale(di, age) && ext == "") {
		removeItem(fp, rootFolder, dryRun)
	}
}
Missing combined test

There is no test covering --prune-empty-dirs used together with a non-empty ext filter. Consider adding a test to verify combined behavior.

// TestPruneEmptyDirsOption tests the prune-empty-dirs option
func (suite *RMStateSuite) TestPruneEmptyDirsOption() {
	// Create a new empty subdirectory that is recent (should normally be kept)
	recentEmptySubdir := tempDirectory(suite.T(), "recentEmptySubdir", suite.rootDir)
	setAge(recentEmptySubdir, suite.age-4)

	// Run procDir with pruneEmptyDirs = true
	err := procDir(suite.rootDir, suite.rootDir, suite.age, "", false, true)
	// Ensure that err == nil
	suite.Nil(err)

	// Check that the recent empty subdirectory is removed
	suite.False(exists(recentEmptySubdir))

	// Check that other empty directories (even recent ones) are also removed
	suite.False(exists(suite.recentSubdir1))

	// Check that non-empty directories are still retained
	suite.True(exists(suite.oldSubdir3))
}

💡 Tool usage guide:

Overview:
The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

  • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
/review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
[pr_reviewer]
some_config1=...
some_config2=...

See the review usage page for a comprehensive guide on using this tool.

@sophie-syntax
Copy link
Contributor

sophie-syntax bot commented Jan 20, 2026

PR Code Suggestions ✨

No code suggestions found for the PR.

@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

Add --prune-empty-dirs option to remove empty directories regardless of age

1 participant