Skip to content

Commit 44626f2

Browse files
qdrivenclaude
andcommitted
add flush-dns tests for all platforms and refactor command definitions
Extract getDNSCommands() to separate command definitions from execution, enabling testable platform-specific DNS flush commands for macOS, Windows, and Linux. Add BDD-style tests using Ginkgo/Gomega. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8e9c2eb commit 44626f2

File tree

3 files changed

+105
-12
lines changed

3 files changed

+105
-12
lines changed

cmd/magic/flush_dns.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ Platform-specific commands used:
3030
},
3131
}
3232

33+
func getDNSCommands(osName string) ([][]string, error) {
34+
switch osName {
35+
case "darwin":
36+
return [][]string{
37+
{"sudo", "dscacheutil", "-flushcache"},
38+
{"sudo", "killall", "-HUP", "mDNSResponder"},
39+
}, nil
40+
case "windows":
41+
return [][]string{
42+
{"ipconfig", "/flushdns"},
43+
}, nil
44+
case "linux":
45+
return [][]string{
46+
{"sudo", "systemctl", "restart", "systemd-resolved"},
47+
{"sudo", "service", "nscd", "restart"},
48+
{"sudo", "service", "dnsmasq", "restart"},
49+
{"sudo", "rndc", "flush"},
50+
}, nil
51+
default:
52+
return nil, fmt.Errorf("unsupported operating system: %s", osName)
53+
}
54+
}
55+
3356
func flushDNS() error {
3457
osName := runtime.GOOS
3558

@@ -51,10 +74,7 @@ func flushDNS() error {
5174
func flushDNSMac() error {
5275
pterm.Info.Println("Flushing DNS cache on macOS...")
5376

54-
commands := [][]string{
55-
{"sudo", "dscacheutil", "-flushcache"},
56-
{"sudo", "killall", "-HUP", "mDNSResponder"},
57-
}
77+
commands, _ := getDNSCommands("darwin")
5878

5979
for _, cmdArgs := range commands {
6080
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
@@ -74,7 +94,10 @@ func flushDNSMac() error {
7494
func flushDNSWindows() error {
7595
pterm.Info.Println("Flushing DNS cache on Windows...")
7696

77-
cmd := exec.Command("ipconfig", "/flushdns")
97+
commands, _ := getDNSCommands("windows")
98+
cmdArgs := commands[0]
99+
100+
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
78101
cmd.Stdout = os.Stdout
79102
cmd.Stderr = os.Stderr
80103

@@ -89,15 +112,10 @@ func flushDNSWindows() error {
89112
func flushDNSLinux() error {
90113
pterm.Info.Println("Flushing DNS cache on Linux...")
91114

92-
flushMethods := [][]string{
93-
{"sudo", "systemctl", "restart", "systemd-resolved"},
94-
{"sudo", "service", "nscd", "restart"},
95-
{"sudo", "service", "dnsmasq", "restart"},
96-
{"sudo", "rndc", "flush"},
97-
}
115+
commands, _ := getDNSCommands("linux")
98116

99117
var lastErr error
100-
for _, cmdArgs := range flushMethods {
118+
for _, cmdArgs := range commands {
101119
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
102120
cmd.Stdout = os.Stdout
103121
cmd.Stderr = os.Stderr

cmd/magic/flush_dns_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package magic
2+
3+
import (
4+
"runtime"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
var _ = Describe("FlushDNS", func() {
11+
Describe("getDNSCommands", func() {
12+
Context("on macOS", func() {
13+
It("should return dscacheutil and killall commands", func() {
14+
commands, err := getDNSCommands("darwin")
15+
Expect(err).NotTo(HaveOccurred())
16+
Expect(commands).To(HaveLen(2))
17+
Expect(commands[0]).To(Equal([]string{"sudo", "dscacheutil", "-flushcache"}))
18+
Expect(commands[1]).To(Equal([]string{"sudo", "killall", "-HUP", "mDNSResponder"}))
19+
})
20+
})
21+
22+
Context("on Windows", func() {
23+
It("should return ipconfig flushdns command", func() {
24+
commands, err := getDNSCommands("windows")
25+
Expect(err).NotTo(HaveOccurred())
26+
Expect(commands).To(HaveLen(1))
27+
Expect(commands[0]).To(Equal([]string{"ipconfig", "/flushdns"}))
28+
})
29+
})
30+
31+
Context("on Linux", func() {
32+
It("should return multiple flush methods", func() {
33+
commands, err := getDNSCommands("linux")
34+
Expect(err).NotTo(HaveOccurred())
35+
Expect(commands).To(HaveLen(4))
36+
Expect(commands[0]).To(Equal([]string{"sudo", "systemctl", "restart", "systemd-resolved"}))
37+
Expect(commands[1]).To(Equal([]string{"sudo", "service", "nscd", "restart"}))
38+
Expect(commands[2]).To(Equal([]string{"sudo", "service", "dnsmasq", "restart"}))
39+
Expect(commands[3]).To(Equal([]string{"sudo", "rndc", "flush"}))
40+
})
41+
})
42+
43+
Context("on unsupported OS", func() {
44+
It("should return an error", func() {
45+
_, err := getDNSCommands("freebsd")
46+
Expect(err).To(HaveOccurred())
47+
Expect(err.Error()).To(ContainSubstring("unsupported operating system: freebsd"))
48+
})
49+
})
50+
})
51+
52+
Describe("flushDNS routing", func() {
53+
Context("on the current platform", func() {
54+
It("should not return unsupported OS error", func() {
55+
err := flushDNS()
56+
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" || runtime.GOOS == "linux" {
57+
Expect(err).NotTo(HaveOccurred())
58+
}
59+
})
60+
})
61+
})
62+
})

cmd/magic/magic_suite_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package magic
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestMagic(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Magic Suite")
13+
}

0 commit comments

Comments
 (0)