Clean up code reuse, consistency, and efficiency issues
Merge readExistingSerial and readExistingContent into a single readExisting function to eliminate duplicate file I/O. Extract dateBase helper to deduplicate serial formula between defaultSerial and bumpSerial. Cache hash results during collision check to avoid recomputing per member. Normalize error prefixes (remove "error:" from fmt.Errorf, add uniformly at print sites). Use filepath.Join instead of manual "/" concatenation. Replace trivial containsStr wrapper with strings.Contains. Simplify tokenize to a single return. Use writeTestFile and fixedTime helpers consistently in tests.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -159,12 +161,12 @@ func TestGenerateCatalogZone(t *testing.T) {
|
||||
aIdx := -1
|
||||
bIdx := -1
|
||||
for i, line := range lines {
|
||||
if containsStr(line, "a.example.com.") {
|
||||
if strings.Contains(line, "a.example.com.") {
|
||||
if aIdx == -1 {
|
||||
aIdx = i
|
||||
}
|
||||
}
|
||||
if containsStr(line, "b.example.com.") {
|
||||
if strings.Contains(line, "b.example.com.") {
|
||||
if bIdx == -1 {
|
||||
bIdx = i
|
||||
}
|
||||
@@ -180,7 +182,7 @@ func TestGenerateCatalogZone(t *testing.T) {
|
||||
// a.example.com should have a group TXT
|
||||
foundGroup := false
|
||||
for _, line := range lines {
|
||||
if containsStr(line, "group.") && containsStr(line, "\"mygroup\"") {
|
||||
if strings.Contains(line, "group.") && strings.Contains(line, "\"mygroup\"") {
|
||||
foundGroup = true
|
||||
}
|
||||
}
|
||||
@@ -211,7 +213,7 @@ func TestGenerateCatalogZoneCOO(t *testing.T) {
|
||||
|
||||
foundCOO := false
|
||||
for _, line := range splitLines(content) {
|
||||
if containsStr(line, "coo.") && containsStr(line, "old.example.com.") {
|
||||
if strings.Contains(line, "coo.") && strings.Contains(line, "old.example.com.") {
|
||||
foundCOO = true
|
||||
}
|
||||
}
|
||||
@@ -220,69 +222,51 @@ func TestGenerateCatalogZoneCOO(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadExistingSerial(t *testing.T) {
|
||||
func TestReadExisting(t *testing.T) {
|
||||
t.Run("file does not exist", func(t *testing.T) {
|
||||
serial, err := readExistingSerial("/nonexistent/path.zone")
|
||||
content, serial, err := readExisting("/nonexistent/path.zone")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if serial != 0 {
|
||||
t.Errorf("serial = %d, want 0", serial)
|
||||
}
|
||||
if content != "" {
|
||||
t.Errorf("content = %q, want empty", content)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("valid zone file", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := dir + "/test.zone"
|
||||
content := "example.com. 0 IN SOA ns1.example.com. hostmaster.example.com. 2026030205 900 600 2147483646 0\nexample.com. 0 IN NS invalid.\n"
|
||||
writeTestFile(t, dir, "test.zone", content)
|
||||
zoneContent := "example.com. 0 IN SOA ns1.example.com. hostmaster.example.com. 2026030205 900 600 2147483646 0\nexample.com. 0 IN NS invalid.\n"
|
||||
writeTestFile(t, dir, "test.zone", zoneContent)
|
||||
|
||||
serial, err := readExistingSerial(path)
|
||||
content, serial, err := readExisting(filepath.Join(dir, "test.zone"))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if serial != 2026030205 {
|
||||
t.Errorf("serial = %d, want 2026030205", serial)
|
||||
}
|
||||
if content != zoneContent {
|
||||
t.Errorf("content mismatch: got %q", content)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("zone file with no SOA", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := dir + "/test.zone"
|
||||
content := "example.com. 0 IN NS invalid.\n"
|
||||
writeTestFile(t, dir, "test.zone", content)
|
||||
zoneContent := "example.com. 0 IN NS invalid.\n"
|
||||
writeTestFile(t, dir, "test.zone", zoneContent)
|
||||
|
||||
serial, err := readExistingSerial(path)
|
||||
content, serial, err := readExisting(filepath.Join(dir, "test.zone"))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if serial != 0 {
|
||||
t.Errorf("serial = %d, want 0 (no SOA found)", serial)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestReadExistingContent(t *testing.T) {
|
||||
t.Run("file does not exist", func(t *testing.T) {
|
||||
content, err := readExistingContent("/nonexistent/path.zone")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if content != "" {
|
||||
t.Errorf("content = %q, want empty", content)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("file exists", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeTestFile(t, dir, "test.zone", "hello\n")
|
||||
|
||||
content, err := readExistingContent(dir + "/test.zone")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if content != "hello\n" {
|
||||
t.Errorf("content = %q, want %q", content, "hello\n")
|
||||
if content != zoneContent {
|
||||
t.Errorf("content mismatch: got %q", content)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user