Skip to content

Commit f15c56e

Browse files
committed
fix(e2e): build correct path and use existing ShowStatusBar key
1 parent f293243 commit f15c56e

File tree

2 files changed

+175
-3
lines changed

2 files changed

+175
-3
lines changed

e2e_test.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
"os/exec"
7+
"path/filepath"
8+
"runtime"
9+
"strings"
10+
"testing"
11+
)
12+
13+
// TestE2E runs end-to-end tests for the mdefaults tool.
14+
// These tests are designed to be run in a CI environment and won't affect the host system.
15+
func TestE2E(t *testing.T) {
16+
if testing.Short() {
17+
t.Skip("Skipping E2E tests in short mode")
18+
}
19+
// Skip if not running in CI environment to prevent messing with local settings
20+
if os.Getenv("CI") != "true" {
21+
t.Skip("Skipping E2E tests when not in CI environment")
22+
}
23+
// Skip on non-macOS systems as tests use macOS 'defaults' command
24+
if runtime.GOOS != "darwin" {
25+
t.Skip("Skipping E2E tests on non-macOS systems")
26+
}
27+
28+
// Setup a test directory with a temporary .mdefaults file
29+
homeDir, err := os.UserHomeDir()
30+
if err != nil {
31+
t.Fatalf("Failed to get home directory: %v", err)
32+
}
33+
34+
// Backup original .mdefaults if it exists
35+
originalConfig := filepath.Join(homeDir, ".mdefaults")
36+
backupConfig := filepath.Join(homeDir, ".mdefaults.bak")
37+
38+
hasOriginalConfig := false
39+
if _, err := os.Stat(originalConfig); err == nil {
40+
hasOriginalConfig = true
41+
if err := os.Rename(originalConfig, backupConfig); err != nil {
42+
t.Fatalf("Failed to backup original config: %v", err)
43+
}
44+
defer func() {
45+
if err := os.Remove(originalConfig); err != nil {
46+
log.Printf("Failed to remove test config: %v", err)
47+
}
48+
if hasOriginalConfig {
49+
if err := os.Rename(backupConfig, originalConfig); err != nil {
50+
log.Printf("Failed to restore original config: %v", err)
51+
}
52+
}
53+
}()
54+
}
55+
56+
// Create test config file with test values
57+
testConfig := `com.apple.homeenergyd Migration24
58+
com.apple.iCal CALPrefLastTruthFileMigrationVersion
59+
com.apple.WindowManager LastHeartbeatDateString.daily`
60+
61+
if err := os.WriteFile(originalConfig, []byte(testConfig), 0644); err != nil {
62+
t.Fatalf("Failed to write test config: %v", err)
63+
}
64+
65+
// Build the mdefaults binary
66+
cmd := exec.Command("go", "build", "-o", "mdefaults", "./cmd/mdefaults")
67+
if output, err := cmd.CombinedOutput(); err != nil {
68+
t.Fatalf("Failed to build mdefaults: %v\nOutput: %s", err, output)
69+
}
70+
71+
// Test the pull command
72+
t.Run("PullCommand", func(t *testing.T) {
73+
cmd := exec.Command("./mdefaults", "pull", "-y") // Add -y flag to skip confirmation
74+
output, err := cmd.CombinedOutput()
75+
if err != nil {
76+
t.Fatalf("Failed to execute pull command: %v\nOutput: %s", err, output)
77+
}
78+
79+
// Read the updated config file
80+
updatedConfig, err := os.ReadFile(originalConfig)
81+
if err != nil {
82+
t.Fatalf("Failed to read updated config: %v", err)
83+
}
84+
85+
// Verify that the config has been updated with types
86+
updatedStr := string(updatedConfig)
87+
if !strings.Contains(updatedStr, "-") {
88+
t.Errorf("Expected config to contain type information, but got: %s", updatedStr)
89+
}
90+
91+
// Check that our test keys exist in the updated config
92+
for _, key := range []string{"Migration24", "CALPrefLastTruthFileMigrationVersion", "LastHeartbeatDateString"} {
93+
if !strings.Contains(updatedStr, key) {
94+
t.Errorf("Expected updated config to contain key '%s', but it doesn't", key)
95+
}
96+
}
97+
})
98+
99+
// Test the push command
100+
t.Run("PushCommand", func(t *testing.T) {
101+
// First, modify the config to set predictable test values
102+
testValues := `com.apple.homeenergyd Migration24 1
103+
com.apple.iCal CALPrefLastTruthFileMigrationVersion 2
104+
com.apple.WindowManager LastHeartbeatDateString.daily "hogehoge"`
105+
106+
if err := os.WriteFile(originalConfig, []byte(testValues), 0644); err != nil {
107+
t.Fatalf("Failed to write test values: %v", err)
108+
}
109+
110+
// Run push command
111+
cmd := exec.Command("./mdefaults", "push", "-y") // Add -y flag to skip confirmation if needed
112+
output, err := cmd.CombinedOutput()
113+
if err != nil {
114+
t.Fatalf("Failed to execute push command: %v\nOutput: %s", err, output)
115+
}
116+
117+
// Verify the values were set correctly using defaults command
118+
for _, tc := range []struct {
119+
domain string
120+
key string
121+
expectedValue string
122+
expectedType string
123+
}{
124+
{"com.apple.homeenergyd", "Migration24", "1", "boolean"},
125+
{"com.apple.iCal", "CALPrefLastTruthFileMigrationVersion", "2", "integer"},
126+
{"com.apple.WindowManager", "LastHeartbeatDateString.daily", "hogehoge", "string"},
127+
} {
128+
// Check value
129+
valueCmd := exec.Command("defaults", "read", tc.domain, tc.key)
130+
valueOutput, err := valueCmd.CombinedOutput()
131+
if err != nil {
132+
t.Errorf("Failed to read %s.%s value: %v\nOutput: %s", tc.domain, tc.key, err, valueOutput)
133+
continue
134+
}
135+
136+
value := strings.TrimSpace(string(valueOutput))
137+
if value != tc.expectedValue {
138+
t.Errorf("Expected %s.%s value to be '%s', but got '%s'", tc.domain, tc.key, tc.expectedValue, value)
139+
}
140+
141+
// Check type
142+
typeCmd := exec.Command("defaults", "read-type", tc.domain, tc.key)
143+
typeOutput, err := typeCmd.CombinedOutput()
144+
if err != nil {
145+
t.Errorf("Failed to read %s.%s type: %v\nOutput: %s", tc.domain, tc.key, err, typeOutput)
146+
continue
147+
}
148+
149+
typeStr := strings.TrimSpace(string(typeOutput))
150+
expectedTypeStr := "Type is " + tc.expectedType
151+
if !strings.Contains(typeStr, expectedTypeStr) {
152+
t.Errorf("Expected %s.%s type to be '%s', but got '%s'", tc.domain, tc.key, expectedTypeStr, typeStr)
153+
}
154+
}
155+
156+
// Clean up by restoring original defaults values
157+
for _, item := range []struct {
158+
domain string
159+
key string
160+
}{
161+
{"com.apple.homeenergyd", "Migration24"},
162+
{"com.apple.iCal", "CALPrefLastTruthFileMigrationVersion"},
163+
{"com.apple.WindowManager", "LastHeartbeatDateString.daily"},
164+
} {
165+
cmd := exec.Command("defaults", "delete", item.domain, item.key)
166+
if err := cmd.Run(); err != nil {
167+
// Just log errors here, as keys might not exist
168+
log.Printf("Warning: Failed to delete %s.%s: %v", item.domain, item.key, err)
169+
}
170+
}
171+
})
172+
}

test/e2e/run_tests.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func main() {
8383

8484
// Test 1: Create a test configuration file
8585
fmt.Println("Test 1: Creating test configuration file")
86-
configContent := "com.apple.dock autohide\ncom.apple.finder ShowPathbar\n"
86+
configContent := "com.apple.dock autohide\ncom.apple.finder ShowStatusBar\n"
8787
if err := os.WriteFile(configFile, []byte(configContent), 0644); err != nil {
8888
log.Fatalf("Failed to create config file: %v", err)
8989
}
@@ -128,7 +128,7 @@ func main() {
128128
}
129129

130130
// Update the configuration file
131-
configContent = fmt.Sprintf("com.apple.dock autohide %s\ncom.apple.finder ShowPathbar\n", newAutohide)
131+
configContent = fmt.Sprintf("com.apple.dock autohide %s\ncom.apple.finder ShowStatusBar\n", newAutohide)
132132
if err := os.WriteFile(configFile, []byte(configContent), 0644); err != nil {
133133
log.Fatalf("Failed to update config file: %v", err)
134134
}
@@ -164,7 +164,7 @@ func main() {
164164

165165
// Test 5: Restore original value
166166
fmt.Println("Test 5: Restoring original value")
167-
configContent = fmt.Sprintf("com.apple.dock autohide %s\ncom.apple.finder ShowPathbar\n", originalAutohide)
167+
configContent = fmt.Sprintf("com.apple.dock autohide %s\ncom.apple.finder ShowStatusBar\n", originalAutohide)
168168
if err := os.WriteFile(configFile, []byte(configContent), 0644); err != nil {
169169
log.Fatalf("Failed to update config file: %v", err)
170170
}

0 commit comments

Comments
 (0)