Skip to content

Commit

Permalink
Add not needed $asset function
Browse files Browse the repository at this point in the history
Code is nearly untestable. fml
  • Loading branch information
nhh committed Dec 3, 2024
1 parent 4b6c3f0 commit 521695e
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
47 changes: 47 additions & 0 deletions internal/ts/testdata/chart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
declare function $assert(value: any): boolean

function bla() {
return true
}

export default () => {

const prom = new Promise((resolve, reject) => {
resolve(true)
})

const prom2 = new Promise((resolve, _) => {
// Simulating a sleep because setTimeout does not exist
for(let i = 1; i < 1000000; i++) {}
resolve(true)
})

const prom3 = new Promise((_, reject) => {
// Simulating a sleep because setTimeout does not exist
for(let i = 1; i < 1000000; i++) {}

reject()
})

// Support
$assert(1)
$assert(true)
$assert(1 === 1)
$assert(bla())

// No support
$assert(() => true) // Use $assert((() => true)) immerdiate invoked function
$assert(bla) // Use $assert(bla()) invoked function
$assert(prom)
$assert(prom2)
$assert(prom3)

return {
namespace: {
kind: "Namespace",
apiVersion: "v1",
metadata: {name: "Test"},
},
components: [],
}
}
35 changes: 35 additions & 0 deletions internal/ts/testdata/panic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
declare function $assert(value: any): boolean

function bla() {
return true
}

export default () => {

const prom3 = new Promise((_, reject) => {
// Simulating a sleep because setTimeout does not exist
for(let i = 1; i < 1000000; i++) {}

reject()
})

// Support
$assert(false)
$assert(null)
$assert(throw "")

// No support
$assert(() => true) // Use $assert((() => true)) immerdiate invoked function
$assert(bla) // Use $assert(bla()) invoked function

$assert(prom3)

return {
namespace: {
kind: "Namespace",
apiVersion: "v1",
metadata: {name: "Test"},
},
components: [],
}
}
44 changes: 44 additions & 0 deletions internal/ts/ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,49 @@ func injectEnv(vm *goja.Runtime) {

}

func __jsAssertFunc(jsValue any) {
switch val := jsValue.(type) {
case uint, uint16, uint8, uint64:
return
case int64, int32, int16, int8, int:
return
case bool:
if val == false {
panic("$assertion evaluated to false: ")
}
return
case func(goja.FunctionCall) goja.Value:
// Unwrap function call
ret := val(goja.FunctionCall{})
__jsAssertFunc(ret)
return
case goja.Value:
return
case *goja.Promise:
if val.State() == goja.PromiseStateFulfilled {
return
}
if val.State() == goja.PromiseStateRejected {
panic("$assertion evaluated to rejected promise: ")
}
return
case string:
return
case nil:
panic("$assertion evaluated to nil: ")
default:
panic("Cannot assert $js.Value")
}
}

func injectAssertFunction(vm *goja.Runtime) {
err := vm.Set("$assert", __jsAssertFunc)

if err != nil {
panic("Cannot set $env obj to vm")
}
}

func injectChartInfo(vm *goja.Runtime, path string) {
dir, _ := filepath.Split(path)

Expand Down Expand Up @@ -183,6 +226,7 @@ func Run(code string, path string) map[string]interface{} {
// Todo handle this better because one creates k8x the other expects it to exist
injectEnv(vm)
injectChartInfo(vm, path)
injectAssertFunction(vm)

_, err := vm.RunString(code)

Expand Down
17 changes: 17 additions & 0 deletions internal/ts/ts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ts

import (
"testing"
)

func TestRun(t *testing.T) {
code := Load("./testdata/success.ts", false)
Run(code, ".")

defer func() { _ = recover() }()

code = Load("./testdata/panic.ts", false)
Run(code, ".")

t.Errorf("did not panic")
}

0 comments on commit 521695e

Please sign in to comment.