Files
dcf/pkg/dcf/helpers_internal_test.go
T
clawbot bf4cf5e1c2 detect PRIVATE/M4ROOT and return the requested store count (closes #6)
A card was detected only with M4ROOT at its root, and asking for two or
more stores returned one too few. A mount point now counts when its root
holds a DCIM or PRIVATE/M4ROOT directory, and the count is exact.

The check and the counting are split from the partition listing so tests
can hand them temporary directories.

Model: opus-5-5
2026-11-02 19:33:06 +00:00

86 lines
1.8 KiB
Go

package dcf
import (
"os"
"path/filepath"
"slices"
"testing"
)
// newMountPoint returns a temporary directory holding the given directories.
func newMountPoint(t *testing.T, directories ...string) string {
t.Helper()
root := t.TempDir()
for _, directory := range directories {
err := os.MkdirAll(filepath.Join(root, directory), 0o750)
if err != nil {
t.Fatalf("MkdirAll: %v", err)
}
}
return root
}
func TestIsDCFMountPoint(t *testing.T) {
t.Parallel()
cases := map[string]bool{
"DCIM": true,
"PRIVATE/M4ROOT": true,
"M4ROOT": false,
"PRIVATE": false,
}
for directory, want := range cases {
got := isDCFMountPoint(newMountPoint(t, directory))
if got != want {
t.Errorf("isDCFMountPoint with only %q = %v, want %v",
directory, got, want)
}
}
}
func TestIsDCFMountPointIgnoresPlainFile(t *testing.T) {
t.Parallel()
root := t.TempDir()
err := os.WriteFile(filepath.Join(root, "DCIM"), nil, 0o600)
if err != nil {
t.Fatalf("WriteFile: %v", err)
}
if isDCFMountPoint(root) {
t.Fatal("isDCFMountPoint counted a plain file named DCIM as a directory")
}
}
func TestFilterDCFMountPointsReturnsRequestedCount(t *testing.T) {
t.Parallel()
first := newMountPoint(t, "DCIM")
second := newMountPoint(t, "PRIVATE/M4ROOT")
third := newMountPoint(t, "DCIM")
// This one holds no DCF store, so it must not use up the requested count.
mountpoints := []string{newMountPoint(t), first, second, third}
cases := map[int][]string{
0: {first, second, third},
1: {first},
2: {first, second},
3: {first, second, third},
4: {first, second, third},
}
for requestedCount, want := range cases {
got := filterDCFMountPoints(mountpoints, requestedCount)
if !slices.Equal(got, want) {
t.Errorf("filterDCFMountPoints(%d) = %v, want %v",
requestedCount, got, want)
}
}
}