mirror of
https://github.com/tw93/Mole.git
synced 2026-03-22 20:50:06 +00:00
fix(status): correct external disk capacity totals
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/shirou/gopsutil/v4/disk"
|
||||
@@ -58,3 +60,91 @@ func TestShouldSkipDiskPartition(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractPlistUint(t *testing.T) {
|
||||
t.Run("prefers first matching key", func(t *testing.T) {
|
||||
raw := `<plist><dict>
|
||||
<key>TotalSize</key><integer>1099511627776</integer>
|
||||
<key>DiskSize</key><integer>2199023255552</integer>
|
||||
</dict></plist>`
|
||||
|
||||
got, err := extractPlistUint(raw, "TotalSize", "DiskSize")
|
||||
if err != nil {
|
||||
t.Fatalf("extractPlistUint() error = %v", err)
|
||||
}
|
||||
if got != 1099511627776 {
|
||||
t.Fatalf("extractPlistUint() = %d, want %d", got, uint64(1099511627776))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("falls back to later keys", func(t *testing.T) {
|
||||
raw := `<plist><dict><key>DiskSize</key><integer>1099511627776</integer></dict></plist>`
|
||||
|
||||
got, err := extractPlistUint(raw, "TotalSize", "DiskSize", "Size")
|
||||
if err != nil {
|
||||
t.Fatalf("extractPlistUint() error = %v", err)
|
||||
}
|
||||
if got != 1099511627776 {
|
||||
t.Fatalf("extractPlistUint() = %d, want %d", got, uint64(1099511627776))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("returns error for malformed integer", func(t *testing.T) {
|
||||
raw := `<plist><dict><key>TotalSize</key><integer>oops</integer></dict></plist>`
|
||||
|
||||
if _, err := extractPlistUint(raw, "TotalSize"); err == nil {
|
||||
t.Fatalf("extractPlistUint() expected parse error")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCorrectDiskTotalBytes(t *testing.T) {
|
||||
origRunCmd := runCmd
|
||||
origCommandExists := commandExists
|
||||
t.Cleanup(func() {
|
||||
runCmd = origRunCmd
|
||||
commandExists = origCommandExists
|
||||
})
|
||||
|
||||
commandExists = func(name string) bool {
|
||||
return name == "diskutil"
|
||||
}
|
||||
|
||||
t.Run("uses diskutil total when meaningfully different", func(t *testing.T) {
|
||||
runCmd = func(ctx context.Context, name string, args ...string) (string, error) {
|
||||
if name != "diskutil" {
|
||||
return "", errors.New("unexpected command")
|
||||
}
|
||||
return `<plist><dict><key>TotalSize</key><integer>1099511627776</integer></dict></plist>`, nil
|
||||
}
|
||||
|
||||
got := correctDiskTotalBytes("/Volumes/Backup", 2199023255552)
|
||||
if got != 1099511627776 {
|
||||
t.Fatalf("correctDiskTotalBytes() = %d, want %d", got, uint64(1099511627776))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("keeps raw total for small differences", func(t *testing.T) {
|
||||
runCmd = func(ctx context.Context, name string, args ...string) (string, error) {
|
||||
return `<plist><dict><key>TotalSize</key><integer>1000500000000</integer></dict></plist>`, nil
|
||||
}
|
||||
|
||||
const rawTotal = 1000000000000
|
||||
got := correctDiskTotalBytes("/Volumes/FastSSD", rawTotal)
|
||||
if got != rawTotal {
|
||||
t.Fatalf("correctDiskTotalBytes() = %d, want %d", got, uint64(rawTotal))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("keeps raw total when diskutil fails", func(t *testing.T) {
|
||||
runCmd = func(ctx context.Context, name string, args ...string) (string, error) {
|
||||
return "", errors.New("diskutil failed")
|
||||
}
|
||||
|
||||
const rawTotal = 1099511627776
|
||||
got := correctDiskTotalBytes("/Volumes/FastSSD", rawTotal)
|
||||
if got != rawTotal {
|
||||
t.Fatalf("correctDiskTotalBytes() = %d, want %d", got, uint64(rawTotal))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user