Skip to content
Merged

Chdir #166

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `gi:load-system` function.
- Added support for shebang at the start of a file.
- Added support for ASDF style system component definitions.
- Added `gi:getcwd` function.
- Added `gi:chdir` function.

### Fixed
- The CompileList function now correctly handles package prefixes.
Expand Down
1 change: 1 addition & 0 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ func (obj *Package) Describe(b []byte, indent, right int, ansi bool) []byte {
b = append(b, indentSpaces[:mx-len(k)+1]...)
}
}
b = append(b, '\n')
}
if 0 < len(obj.classes) {
names = names[:0]
Expand Down
2 changes: 1 addition & 1 deletion pkg/cl/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {
},
},
Return: "list",
Text: `__trace__ turns tracing on for the listed function _name*. If the only name is _t_ then
Text: `__trace__ turns tracing on for the listed function _name_. If the only name is _t_ then
tracing is turned on for all functions. If no functions are named then a list of the functions being
traced is returned.`,
Examples: []string{
Expand Down
4 changes: 2 additions & 2 deletions pkg/cl/typecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ func (f *Typecase) Call(s *slip.Scope, args slip.List, depth int) (result slip.O
}

func typecaseMatch(sym slip.Symbol, key slip.Object) bool {
if strings.EqualFold("null", string(sym)) && key == nil {
return true
if key == nil {
return strings.EqualFold("null", string(sym))
}
for _, h := range key.Hierarchy() {
if strings.EqualFold(string(h), string(sym)) {
Expand Down
48 changes: 48 additions & 0 deletions pkg/gi/chdir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2026, Peter Ohler, All rights reserved.

package gi

import (
"os"

"github.com/ohler55/slip"
)

func defChdir() {
slip.Define(
func(args slip.List) slip.Object {
f := Chdir{Function: slip.Function{Name: "chdir", Args: args}}
f.Self = &f
return &f
},
&slip.FuncDoc{
Name: "chdir",
Args: []*slip.DocArg{
{
Name: "path",
Type: "string",
Text: "The directory to change to.",
},
},
Return: "nil",
Text: `__chdir__ changes the current directory to _path_.`,
}, &Pkg)
}

// Chdir represents the chdir function.
type Chdir struct {
slip.Function
}

// Call the function with the arguments provided.
func (f *Chdir) Call(s *slip.Scope, args slip.List, depth int) slip.Object {
slip.CheckArgCount(s, depth, f, args, 1, 1)
vs, ok := args[0].(slip.String)
if !ok {
slip.TypePanic(s, depth, "path", args[0], "string")
}
if err := os.Chdir(string(vs)); err != nil {
panic(err)
}
return nil
}
37 changes: 37 additions & 0 deletions pkg/gi/getcwd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2026, Peter Ohler, All rights reserved.

package gi

import (
"os"

"github.com/ohler55/slip"
)

func defGetcwd() {
slip.Define(
func(args slip.List) slip.Object {
f := Getcwd{Function: slip.Function{Name: "getcwd", Args: args}}
f.Self = &f
return &f
},
&slip.FuncDoc{
Name: "getcwd",
Args: []*slip.DocArg{},
Return: "string",
Text: `__getcwd__ return the current directory.`,
}, &Pkg)
}

// Getcwd represents the getcwd function.
type Getcwd struct {
slip.Function
}

// Call the function with the arguments provided.
func (f *Getcwd) Call(s *slip.Scope, args slip.List, depth int) slip.Object {
slip.CheckArgCount(s, depth, f, args, 0, 0)
dir, _ := os.Getwd()

return slip.String(dir)
}
2 changes: 2 additions & 0 deletions pkg/gi/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ func init() {
vv := Pkg.GetVarVal(f.Name())
vv.Const = true
}
defChdir()
defGetcwd()
defProcess()
defFindProcess()
defCommand()
Expand Down
35 changes: 35 additions & 0 deletions test/gi/chdir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2026, Peter Ohler, All rights reserved.

package gi_test

import (
"os"
"testing"

"github.com/ohler55/slip"
"github.com/ohler55/slip/sliptest"
)

func TestChdirOk(t *testing.T) {
dir, _ := os.Getwd()
defer func() { _ = os.Chdir(dir) }()

(&sliptest.Function{
Source: `(progn (chdir "..") (getcwd))`,
Expect: `/slip\/test"$/`,
}).Test(t)
}

func TestChdirBadPath(t *testing.T) {
(&sliptest.Function{
Source: `(chdir t)`,
PanicType: slip.TypeErrorSymbol,
}).Test(t)
}

func TestChdirError(t *testing.T) {
(&sliptest.Function{
Source: `(chdir "....")`,
PanicType: slip.ErrorSymbol,
}).Test(t)
}
16 changes: 16 additions & 0 deletions test/gi/getcwd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2026, Peter Ohler, All rights reserved.

package gi_test

import (
"testing"

"github.com/ohler55/slip/sliptest"
)

func TestGetcwd(t *testing.T) {
(&sliptest.Function{
Source: `(getcwd)`,
Expect: `/slip\/test\/gi"$/`,
}).Test(t)
}
Loading