File tree Expand file tree Collapse file tree 6 files changed +74
-1
lines changed Expand file tree Collapse file tree 6 files changed +74
-1
lines changed Original file line number Diff line number Diff line change @@ -26,4 +26,12 @@ if not(page_size > 0) then error("bad pagesize") end
2626goos .mkdir_all (" ./test/test_dir/test_dir/all" )
2727local stat , err = goos .stat (" ./test/test_dir/test_dir/all" )
2828if err then error (err ) end
29+
30+ -- environ
31+ local env = goos .environ ()
32+ print (env .PATH ) -- prints the PATH environment variable
33+ print (env .HOME ) -- prints the HOME environment variable
34+ for key , value in pairs (env ) do
35+ print (key .. " =" .. value )
36+ end
2937```
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package goos
33
44import (
55 "os"
6+ "strings"
67
78 lua "github.com/yuin/gopher-lua"
89)
@@ -52,3 +53,15 @@ func MkdirAll(L *lua.LState) int {
5253 }
5354 return 0
5455}
56+
57+ // Environ lua goos.environ() returns table
58+ func Environ (L * lua.LState ) int {
59+ envVars := os .Environ ()
60+ result := L .NewTable ()
61+ for _ , env := range envVars {
62+ parts := strings .SplitN (env , "=" , 2 )
63+ result .RawSetString (parts [0 ], lua .LString (parts [1 ]))
64+ }
65+ L .Push (result )
66+ return 1
67+ }
Original file line number Diff line number Diff line change 11package goos
22
33import (
4+ "os"
5+ "testing"
6+
47 "github.com/stretchr/testify/assert"
58 "github.com/vadv/gopher-lua-libs/tests"
6- "testing"
79
810 runtime "github.com/vadv/gopher-lua-libs/runtime"
911)
1012
1113func TestApi (t * testing.T ) {
14+ os .Setenv ("ENV_VAR" , "TEST=1" )
15+ defer os .Unsetenv ("ENV_VAR" )
16+
17+ os .Setenv ("EMPTY_VAR" , "" )
18+ defer os .Unsetenv ("EMPTY_VAR" )
19+
1220 preload := tests .SeveralPreloadFuncs (
1321 runtime .Preload ,
1422 Preload ,
Original file line number Diff line number Diff line change @@ -82,3 +82,27 @@ print(err == nil)
8282 // Output:
8383 // true
8484}
85+
86+ // goos.environ()
87+ func ExampleEnviron () {
88+ state := lua .NewState ()
89+ Preload (state )
90+ source := `
91+ local goos = require("goos")
92+ local env = goos.environ()
93+ -- Check that we get a table
94+ print(type(env) == "table")
95+ -- Check that we have at least one environment variable
96+ local count = 0
97+ for k, v in pairs(env) do
98+ count = count + 1
99+ end
100+ print(count > 0)
101+ `
102+ if err := state .DoString (source ); err != nil {
103+ log .Fatal (err .Error ())
104+ }
105+ // Output:
106+ // true
107+ // true
108+ }
Original file line number Diff line number Diff line change @@ -25,4 +25,5 @@ var api = map[string]lua.LGFunction{
2525 "hostname" : Hostname ,
2626 "get_pagesize" : Getpagesize ,
2727 "mkdir_all" : MkdirAll ,
28+ "environ" : Environ ,
2829}
Original file line number Diff line number Diff line change 1919function Test_pagesize (t )
2020 assert (goos .get_pagesize () > 0 , " pagesize" )
2121end
22+
23+ function Test_environ (t )
24+ local env = goos .environ ()
25+ assert (env , " environ should return table" )
26+ -- Check that we get a table with environment variables
27+ local count = 0
28+ for k , v in pairs (env ) do
29+ count = count + 1
30+ assert (type (k ) == " string" , " key should be string" )
31+ assert (type (v ) == " string" , " value should be string" )
32+ end
33+ assert (count > 0 , " environ should return at least one environment variable" )
34+ -- PATH should exist on most systems
35+ assert (env .PATH or env .Path , " PATH environment variable should exist" )
36+ -- Test environment variable with equals sign in value
37+ assert (env .ENV_VAR == " TEST=1" , " ENV_VAR should be TEST=1" )
38+ -- Test environment variable with empty value
39+ assert (env .EMPTY_VAR == " " , " EMPTY_VAR should be empty string" )
40+ end
You can’t perform that action at this time.
0 commit comments