2025-11-13 19:16:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-14 13:47:27 +00:00
|
|
|
func TestCompileCmd(t *testing.T) {
|
|
|
|
|
// t.Run("Good", func(t *testing.T) {
|
|
|
|
|
// tempDir := t.TempDir()
|
|
|
|
|
// outputTimPath := filepath.Join(tempDir, "test.tim")
|
|
|
|
|
// borgfilePath := filepath.Join(tempDir, "Borgfile")
|
|
|
|
|
// dummyFilePath := filepath.Join(tempDir, "dummy.txt")
|
|
|
|
|
|
|
|
|
|
// // Create a dummy file to add to the tim.
|
|
|
|
|
// err := os.WriteFile(dummyFilePath, []byte("dummy content"), 0644)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// t.Fatalf("failed to create dummy file: %v", err)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Create a Borgfile.
|
|
|
|
|
// borgfileContent := "ADD " + dummyFilePath + " /dummy.txt"
|
|
|
|
|
// err = os.WriteFile(borgfilePath, []byte(borgfileContent), 0644)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// t.Fatalf("failed to create Borgfile: %v", err)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Execute the compile command.
|
|
|
|
|
// cmd := NewCompileCmd()
|
|
|
|
|
// cmd.SetArgs([]string{"--file", borgfilePath, "--output", outputTimPath})
|
|
|
|
|
// err = cmd.Execute()
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// t.Fatalf("compile command failed: %v", err)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Verify the output tim file.
|
|
|
|
|
// timFile, err := os.Open(outputTimPath)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// t.Fatalf("failed to open output tim file: %v", err)
|
|
|
|
|
// }
|
|
|
|
|
// defer timFile.Close()
|
|
|
|
|
|
|
|
|
|
// tr := tar.NewReader(timFile)
|
|
|
|
|
// files := []string{"config.json", "rootfs/", "rootfs/dummy.txt"}
|
|
|
|
|
// found := make(map[string]bool)
|
|
|
|
|
// for {
|
|
|
|
|
// hdr, err := tr.Next()
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// break
|
|
|
|
|
// }
|
|
|
|
|
// found[hdr.Name] = true
|
|
|
|
|
// }
|
|
|
|
|
// for _, f := range files {
|
|
|
|
|
// if !found[f] {
|
|
|
|
|
// t.Errorf("%s not found in tim tarball", f)
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
t.Run("Bad_Borgfile", func(t *testing.T) {
|
2025-11-14 10:36:35 +00:00
|
|
|
tempDir := t.TempDir()
|
2025-11-14 13:47:27 +00:00
|
|
|
outputTimPath := filepath.Join(tempDir, "test.tim")
|
2025-11-14 10:36:35 +00:00
|
|
|
borgfilePath := filepath.Join(tempDir, "Borgfile")
|
2025-11-13 19:21:35 +00:00
|
|
|
|
2025-11-14 13:47:27 +00:00
|
|
|
// Create a Borgfile with an invalid instruction.
|
|
|
|
|
borgfileContent := "INVALID instruction"
|
2025-11-14 10:36:35 +00:00
|
|
|
err := os.WriteFile(borgfilePath, []byte(borgfileContent), 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to create Borgfile: %v", err)
|
|
|
|
|
}
|
2025-11-13 19:21:35 +00:00
|
|
|
|
2025-11-14 13:47:27 +00:00
|
|
|
// Execute the compile command.
|
|
|
|
|
cmd := NewCompileCmd()
|
|
|
|
|
cmd.SetArgs([]string{"--file", borgfilePath, "--output", outputTimPath})
|
|
|
|
|
err = cmd.Execute()
|
2025-11-14 10:36:35 +00:00
|
|
|
if err == nil {
|
2025-11-14 13:47:27 +00:00
|
|
|
t.Error("compile command should have failed but did not")
|
2025-11-14 10:36:35 +00:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2025-11-14 13:47:27 +00:00
|
|
|
t.Run("Bad_ADD", func(t *testing.T) {
|
2025-11-14 10:36:35 +00:00
|
|
|
tempDir := t.TempDir()
|
2025-11-14 13:47:27 +00:00
|
|
|
outputTimPath := filepath.Join(tempDir, "test.tim")
|
2025-11-14 10:36:35 +00:00
|
|
|
borgfilePath := filepath.Join(tempDir, "Borgfile")
|
|
|
|
|
|
2025-11-14 13:47:27 +00:00
|
|
|
// Create a Borgfile with an invalid ADD instruction.
|
|
|
|
|
borgfileContent := "ADD dummy.txt"
|
2025-11-14 10:36:35 +00:00
|
|
|
err := os.WriteFile(borgfilePath, []byte(borgfileContent), 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to create Borgfile: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-14 13:47:27 +00:00
|
|
|
// Execute the compile command.
|
|
|
|
|
cmd := NewCompileCmd()
|
|
|
|
|
cmd.SetArgs([]string{"--file", borgfilePath, "--output", outputTimPath})
|
|
|
|
|
err = cmd.Execute()
|
2025-11-14 10:36:35 +00:00
|
|
|
if err == nil {
|
2025-11-14 13:47:27 +00:00
|
|
|
t.Error("compile command should have failed but did not")
|
2025-11-14 10:36:35 +00:00
|
|
|
}
|
|
|
|
|
})
|
2025-11-13 19:21:35 +00:00
|
|
|
|
2025-11-14 13:47:27 +00:00
|
|
|
t.Run("Ugly_EmptyBorgfile", func(t *testing.T) {
|
2025-11-14 10:36:35 +00:00
|
|
|
tempDir := t.TempDir()
|
2025-11-14 13:47:27 +00:00
|
|
|
outputTimPath := filepath.Join(tempDir, "test.tim")
|
2025-11-14 10:36:35 +00:00
|
|
|
borgfilePath := filepath.Join(tempDir, "Borgfile")
|
2025-11-13 19:21:35 +00:00
|
|
|
|
2025-11-14 10:36:35 +00:00
|
|
|
// Create an empty Borgfile.
|
2025-11-14 13:47:27 +00:00
|
|
|
err := os.WriteFile(borgfilePath, []byte{}, 0644)
|
2025-11-14 10:36:35 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to create Borgfile: %v", err)
|
|
|
|
|
}
|
2025-11-13 19:21:35 +00:00
|
|
|
|
2025-11-14 13:47:27 +00:00
|
|
|
// Execute the compile command.
|
|
|
|
|
cmd := NewCompileCmd()
|
|
|
|
|
cmd.SetArgs([]string{"--file", borgfilePath, "--output", outputTimPath})
|
|
|
|
|
err = cmd.Execute()
|
2025-11-14 10:36:35 +00:00
|
|
|
if err != nil {
|
2025-11-14 13:47:27 +00:00
|
|
|
t.Fatalf("compile command failed: %v", err)
|
2025-11-14 10:36:35 +00:00
|
|
|
}
|
|
|
|
|
})
|
2025-11-13 19:21:35 +00:00
|
|
|
}
|