1
0
Fork 0

Adding upstream version 0.0~git20250409.f7acab6.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-22 11:36:18 +02:00
parent b9b5d88025
commit 21b930d007
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
51 changed files with 11229 additions and 0 deletions

37
process/module.go Normal file
View file

@ -0,0 +1,37 @@
package process
import (
"os"
"strings"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/require"
)
const ModuleName = "process"
type Process struct {
env map[string]string
}
func Require(runtime *goja.Runtime, module *goja.Object) {
p := &Process{
env: make(map[string]string),
}
for _, e := range os.Environ() {
envKeyValue := strings.SplitN(e, "=", 2)
p.env[envKeyValue[0]] = envKeyValue[1]
}
o := module.Get("exports").(*goja.Object)
o.Set("env", p.env)
}
func Enable(runtime *goja.Runtime) {
runtime.Set("process", require.Require(runtime, ModuleName))
}
func init() {
require.RegisterCoreModule(ModuleName, Require)
}

68
process/module_test.go Normal file
View file

@ -0,0 +1,68 @@
package process
import (
"fmt"
"os"
"strings"
"testing"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/require"
)
func TestProcessEnvStructure(t *testing.T) {
vm := goja.New()
new(require.Registry).Enable(vm)
Enable(vm)
if c := vm.Get("process"); c == nil {
t.Fatal("process not found")
}
if c, err := vm.RunString("process.env"); c == nil || err != nil {
t.Fatal("error accessing process.env")
}
}
func TestProcessEnvValuesArtificial(t *testing.T) {
os.Setenv("GOJA_IS_AWESOME", "true")
defer os.Unsetenv("GOJA_IS_AWESOME")
vm := goja.New()
new(require.Registry).Enable(vm)
Enable(vm)
jsRes, err := vm.RunString("process.env['GOJA_IS_AWESOME']")
if err != nil {
t.Fatalf("Error executing: %s", err)
}
if jsRes.String() != "true" {
t.Fatalf("Error executing: got %s but expected %s", jsRes, "true")
}
}
func TestProcessEnvValuesBrackets(t *testing.T) {
vm := goja.New()
new(require.Registry).Enable(vm)
Enable(vm)
for _, e := range os.Environ() {
envKeyValue := strings.SplitN(e, "=", 2)
jsExpr := fmt.Sprintf("process.env['%s']", envKeyValue[0])
jsRes, err := vm.RunString(jsExpr)
if err != nil {
t.Fatalf("Error executing %s: %s", jsExpr, err)
}
if jsRes.String() != envKeyValue[1] {
t.Fatalf("Error executing %s: got %s but expected %s", jsExpr, jsRes, envKeyValue[1])
}
}
}