Skip to content

Commit

Permalink
Merge pull request #15 from Eclalang/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Axou89 authored Apr 24, 2024
2 parents 2b5bbd9 + 46778c4 commit c095322
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

| Func Name | Prototype | Description | Comments |
|:--------------:|:---------------------------------------------:|:--------------------------------------------------------------------:|:-------------------------:|
| Chmod | Chmod(name string, mode int) error {} | Changes the mode of the file | N/A |
| Chown | Chown(path string, uid int, gid int) {} | Chown changes the owner and group of the file | Does not work on Windows |
| ClearEnv | ClearEnv() {} | Clears all environment variables | N/A |
| Create | Create(name string) {} | Creates a file and returns the file object | N/A |
| Exit | Exit(code int) {} | Exits the program with the given code | N/A |
| Getegid | Getegid() int {} | Gets the effective group ID of the calling process | N/A |
| GetEnv | GetEnv(key string) string {} | Returns an environment variable | N/A |
| Geteuid | Geteuid() int {} | Gets the effective user ID of the calling process | N/A |
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/Eclalang/os

go 1.18
go 1.21.1

require github.com/Eclalang/Ecla v1.0.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/Eclalang/Ecla v1.0.0 h1:JrbxU5m6hWfJ8D5/xsqtbhWljOOetDdV6JzEU82/4Fc=
github.com/Eclalang/Ecla v1.0.0/go.mod h1:0Nq24TRfBRBY5oKK+U2hpdzQV0rlisPQZ5nhBLNGxAU=
31 changes: 31 additions & 0 deletions os.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package os

import (
"github.com/Eclalang/Ecla/interpreter/eclaType"
"os"
"strings"
)

var Variables = map[string]eclaType.Type{}

var exit = os.Exit
var oldExit = os.Exit

// Chmod changes the mode of the file
func Chmod(name string, mode int) error {
err := os.Chmod(name, os.FileMode(mode))
if err != nil {
return err
}
return nil
}

// Chown changes the owner and group of the file
func Chown(path string, uid int, gid int) {
os.Chown(path, uid, gid)
Expand All @@ -25,6 +40,17 @@ func Create(name string) {
return
}

// Exit exits the program with the given code
func Exit(code int) {
exit(code)
}

// hookExit is used for testing purpose it hooks the eclaExit variable to the function passed as parameter
func hookExit(f func(int)) {
oldExit = exit
exit = f
}

// Getegid gets the effective group ID of the calling process
func Getegid() int {
return os.Getegid()
Expand Down Expand Up @@ -115,6 +141,11 @@ func RemoveAll(path string) {
os.RemoveAll(path)
}

// restoreExit is used for testing purpose it restore the hook of eclaExit
func restoreExit() {
exit = oldExit
}

// SetEnv sets an environment variable
func SetEnv(key string, value string) {
os.Setenv(key, value)
Expand Down
27 changes: 27 additions & 0 deletions os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ import (
"testing"
)

func TestChmod(t *testing.T) {
// Test working mode
err := Chmod("unit_test_files/test.txt", 0644)
if err != nil {
t.Errorf("Expected nil, got %v", err)
}

// Test error
err = Chmod("unit_test_files/test.t", 0724)
if err == nil {
t.Errorf("Expected an error, got nil")
}
}

func TestChown(t *testing.T) {
oldUid := Getuid()
oldGid := Getgid()
Expand Down Expand Up @@ -56,6 +70,19 @@ func TestCreate(t *testing.T) {
}
}

func TestExit(t *testing.T) {
var ok bool
exit = func(code int) {
ok = code == 1
}
hookExit(exit)
Exit(1)
restoreExit()
if !ok {
t.Errorf("Exit() did not panic")
}
}

func TestGetegid(t *testing.T) {
expected := os.Getegid()
actual := Getegid()
Expand Down

0 comments on commit c095322

Please sign in to comment.