-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
99 lines (86 loc) · 2.42 KB
/
main_test.go
File metadata and controls
99 lines (86 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestNormalizeServer(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"juliahub.com", "juliahub.com"},
{"custom.dev", "custom.dev"},
{"internal", "internal.juliahub.com"},
{"test", "test.juliahub.com"},
}
for _, test := range tests {
result := normalizeServer(test.input)
if result != test.expected {
t.Errorf("normalizeServer(%s) = %s, expected %s", test.input, result, test.expected)
}
}
}
func TestGetConfigFilePath(t *testing.T) {
path := getConfigFilePath()
if !strings.HasSuffix(path, ".juliahub") {
t.Errorf("Config file path should end with .juliahub, got: %s", path)
}
if !filepath.IsAbs(path) {
t.Errorf("Config file path should be absolute, got: %s", path)
}
}
func TestReadConfigFileDefault(t *testing.T) {
// Test reading from non-existent config file should return default
tempHome, err := os.MkdirTemp("", "jh-test")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempHome)
// Temporarily set HOME to our temp directory
origHome := os.Getenv("HOME")
os.Setenv("HOME", tempHome)
defer os.Setenv("HOME", origHome)
server, err := readConfigFile()
if err != nil {
t.Errorf("readConfigFile() should not error on missing file, got: %v", err)
}
if server != "juliahub.com" {
t.Errorf("readConfigFile() should return default server, got: %s", server)
}
}
func TestWriteAndReadConfigFile(t *testing.T) {
tempHome, err := os.MkdirTemp("", "jh-test")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempHome)
// Temporarily set HOME to our temp directory
origHome := os.Getenv("HOME")
os.Setenv("HOME", tempHome)
defer os.Setenv("HOME", origHome)
testServer := "test.juliahub.com"
// Write config file
err = writeConfigFile(testServer)
if err != nil {
t.Errorf("writeConfigFile() failed: %v", err)
}
// Read it back
server, err := readConfigFile()
if err != nil {
t.Errorf("readConfigFile() failed: %v", err)
}
if server != testServer {
t.Errorf("readConfigFile() returned %s, expected %s", server, testServer)
}
// Check file permissions
configPath := getConfigFilePath()
info, err := os.Stat(configPath)
if err != nil {
t.Errorf("Failed to stat config file: %v", err)
}
if info.Mode().Perm() != 0600 {
t.Errorf("Config file permissions should be 0600, got %o", info.Mode().Perm())
}
}