Skip to content

Commit

Permalink
Fail structure validation if any directory is empty
Browse files Browse the repository at this point in the history
Added logic to the validate SIP structure activity to detect empty
directories and fail validation if they exist.
  • Loading branch information
mcantelon committed Jan 20, 2025
1 parent de96836 commit 6ce1a0d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
37 changes: 37 additions & 0 deletions internal/activities/validate_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package activities
import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"slices"
Expand Down Expand Up @@ -34,6 +35,42 @@ func (a *ValidateStructure) Execute(
) (*ValidateStructureResult, error) {
var failures []string

// Check for empty directories.
paths := make(map[string]int)

err := filepath.WalkDir(params.SIP.Path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if path != params.SIP.Path {
relativePath, err := filepath.Rel(params.SIP.Path, path)
if err != nil {
return err
}

// Initialize this directory's total number of immediate children.
if d.IsDir() {
paths[relativePath] = 0
}

// Add to parent's total number of immediate children.
paths[filepath.Dir(relativePath)] = paths[filepath.Dir(relativePath)] + 1
}

return nil
})
if err != nil {
return nil, fmt.Errorf("ValidateStructure: check for empty directories: %v", err)
}

// Report any empty subdirectories.
for path, children := range paths {
if children == 0 {
failures = append(failures, fmt.Sprintf("SIP subdirectory %q is empty", path))
}
}

// Check existence of the content directory.
hasContentDir := true
if !fsutil.FileExists(params.SIP.ContentPath) {
Expand Down
12 changes: 9 additions & 3 deletions internal/activities/validate_structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func TestValidateStructure(t *testing.T) {

unexpectedPiecesSIP, err := sip.New(fs.NewDir(t, "",
fs.WithDir("content",
fs.WithDir("d_0000001"),
fs.WithDir("d_0000001",
fs.WithFile("content.txt", ""),
),
fs.WithFile("unexpected.txt", ""),
),
fs.WithDir("header",
Expand All @@ -72,7 +74,9 @@ func TestValidateStructure(t *testing.T) {
fs.WithFile("arelda.xsd", ""),
),
),
fs.WithDir("unexpected"),
fs.WithDir("unexpected",
fs.WithFile("data.txt", ""),
),
).Path())
assert.NilError(t, err)

Expand All @@ -82,7 +86,9 @@ func TestValidateStructure(t *testing.T) {
missingPiecesAIP, err := sip.New(
fs.NewDir(t, "",
fs.WithDir("AIP-1234",
fs.WithDir("additional"),
fs.WithDir("additional",
fs.WithFile("content.txt", ""),
),
fs.WithFile("Prozess_Digitalisierung_PREMIS.xml", ""),
),
).Join("AIP-1234"),
Expand Down

0 comments on commit 6ce1a0d

Please sign in to comment.