Skip to content

Commit fbd9c4e

Browse files
committed
Some refactoring of gljmain.go to be more idiomatic
1 parent 580116d commit fbd9c4e

File tree

3 files changed

+68
-37
lines changed

3 files changed

+68
-37
lines changed

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
bin
2-
doc/repl/glj.wasm
1+
/.clj-kondo/
2+
/.lsp/
3+
/bin/
4+
/doc/repl/glj.wasm
5+

pkg/gljmain/gljmain.go

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,39 @@ func parseArgs(args []string) (*Args, error) {
9494
}, nil
9595
}
9696

97+
// setupLoadPaths configures the library search path in order of priority
98+
func setupLoadPaths(includePaths []string) {
99+
// Add GLJPATH directories to front of load path if set
100+
loadPaths := os.Getenv("GLJPATH")
101+
if loadPaths != "" {
102+
paths := strings.Split(loadPaths, ":")
103+
for i := len(paths) - 1; i >= 0; i-- {
104+
path := paths[i]
105+
if path != "" {
106+
// Skip non-existent path directories
107+
if _, err := os.Stat(path); err == nil {
108+
runtime.AddLoadPath(os.DirFS(path), true)
109+
}
110+
}
111+
}
112+
}
113+
114+
// Process -I include paths last (add to front of load path, highest priority)
115+
// Process in reverse order so first -I on command line gets highest priority
116+
for i := len(includePaths) - 1; i >= 0; i-- {
117+
path := includePaths[i]
118+
if path != "" {
119+
// Skip non-existent path directories
120+
if _, err := os.Stat(path); err == nil {
121+
runtime.AddLoadPath(os.DirFS(path), true)
122+
}
123+
}
124+
}
125+
126+
// Add current directory to end of load path
127+
runtime.AddLoadPath(os.DirFS("."), false)
128+
}
129+
97130
func printHelp() {
98131
fmt.Printf(`Glojure v%s
99132
@@ -128,35 +161,8 @@ func Main(args []string) {
128161
log.Fatal(err)
129162
}
130163

131-
// Process -I include paths first (add to front of load path)
132-
// Process in reverse order so first -I on command line gets highest priority
133-
for i := len(parsedArgs.IncludePaths) - 1; i >= 0; i-- {
134-
path := parsedArgs.IncludePaths[i]
135-
if path != "" {
136-
// Skip non-existent path directories
137-
if _, err := os.Stat(path); err == nil {
138-
runtime.AddLoadPath(os.DirFS(path), true)
139-
}
140-
}
141-
}
142-
143-
// Add GLJPATH directories to front of load path if set
144-
loadPaths := os.Getenv("GLJPATH")
145-
if loadPaths != "" {
146-
paths := strings.Split(loadPaths, ":")
147-
for i := len(paths) - 1; i >= 0; i-- {
148-
path := paths[i]
149-
if path != "" {
150-
// Skip non-existent path directories
151-
if _, err := os.Stat(path); err == nil {
152-
runtime.AddLoadPath(os.DirFS(path), true)
153-
}
154-
}
155-
}
156-
}
157-
158-
// Add current directory to end of load path
159-
runtime.AddLoadPath(os.DirFS("."), false)
164+
// Setup library search paths
165+
setupLoadPaths(parsedArgs.IncludePaths)
160166

161167
switch parsedArgs.Mode {
162168
case "repl":

test/glojure/test_glojure/cli.glj

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,34 +62,56 @@
6262

6363
(deftest gljpath-test
6464
(test-that
65-
"GLJPATH can load libraries from specified directories"
65+
"GLJPATH and -I flags can load libraries from specified directories"
6666
(let [lib1-dir (str (os.Getenv "PWD") "/test_gljpath_priority1")
6767
lib2-dir (str (os.Getenv "PWD") "/test_gljpath_priority2")
68+
lib3-dir (str (os.Getenv "PWD") "/test_gljpath_priority3")
6869
lib1-file (str lib1-dir "/conflict.glj")
6970
lib2-file (str lib2-dir "/conflict.glj")
71+
lib3-file (str lib3-dir "/conflict.glj")
7072
test-script (str (os.Getenv "PWD") "/test_gljpath_priority_script.glj")
7173
;; Create conflicting libraries with same name but different content
7274
_ (do
7375
(os.MkdirAll lib1-dir 0755)
7476
(os.MkdirAll lib2-dir 0755)
77+
(os.MkdirAll lib3-dir 0755)
7578
(os.WriteFile lib1-file
7679
(str "(ns conflict)\n(defn version [] \"version 1\")")
7780
0644)
7881
(os.WriteFile lib2-file
7982
(str "(ns conflict)\n(defn version [] \"version 2\")")
8083
0644)
84+
(os.WriteFile lib3-file
85+
(str "(ns conflict)\n(defn version [] \"version 3\")")
86+
0644)
8187
(os.WriteFile test-script
8288
(str "(ns main)\n(use 'conflict)\n(println (version))")
8389
0644))
8490
;; Test that first directory in GLJPATH takes precedence
85-
[out err] (run-cli-cmd "sh" "-c"
86-
(str "GLJPATH=" lib1-dir ":" lib2-dir " " glj " " test-script))
91+
[out1 err1] (run-cli-cmd "sh" "-c"
92+
(str "GLJPATH=" lib1-dir ":" lib2-dir " "
93+
glj " " test-script))
94+
;; Test that -I flags take precedence over GLJPATH
95+
[out2 err2] (run-cli-cmd "sh" "-c"
96+
(str "GLJPATH=" lib2-dir ":" lib3-dir " "
97+
glj " -I " lib1-dir " " test-script))
98+
;; Test that first -I flag takes precedence over later ones
99+
[out3 err3] (run-cli-cmd "sh" "-c"
100+
(str glj " -I " lib3-dir " -I " lib2-dir
101+
" -I " lib1-dir " " test-script))
87102
;; Cleanup
88103
_ (do
89104
(os.RemoveAll lib1-dir)
90105
(os.RemoveAll lib2-dir)
106+
(os.RemoveAll lib3-dir)
91107
(os.Remove test-script))]
92-
(is (= out "version 1\n") "First directory in GLJPATH should take precedence")
93-
(is (empty? err) "Command should not return an error"))))
94-
108+
(is (= out1 "version 1\n")
109+
"First directory in GLJPATH should take precedence")
110+
(is (empty? err1) "First command should not return an error")
111+
(is (= out2 "version 1\n")
112+
"-I flag should take precedence over GLJPATH")
113+
(is (empty? err2) "Second command should not return an error")
114+
(is (= out3 "version 3\n")
115+
"First -I flag should take precedence over later ones")
116+
(is (empty? err3) "Third command should not return an error"))))
95117
(run-tests)

0 commit comments

Comments
 (0)