Skip to content

Commit 9672186

Browse files
authored
Ensure that json is not just preloaded but actually loaded so that, when using its methods, it is able to use registered types. (#86)
1 parent 5d5f612 commit 9672186

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

lua.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package main
22

33
import (
44
"fmt"
5+
"io/ioutil"
6+
57
libs "github.com/vadv/gopher-lua-libs"
68
"github.com/vadv/gopher-lua-libs/json"
7-
"io/ioutil"
89

910
"github.com/screwdriver-cd/meta-cli/internal/fetch"
1011
lua "github.com/yuin/gopher-lua"
@@ -341,6 +342,12 @@ func callMethodLGFunction(ud *lua.LUserData, methodName string, nret int) lua.LG
341342
func (l *LuaSpec) initState(L *lua.LState) error {
342343
// Preload the libs libraries
343344
libs.Preload(L)
345+
// Ensure that json is initialized since we use its methods
346+
// Without this, if cli did meta set -j foo {}, then lua's meta.set("foo", meta.get("foo")) would set to [].
347+
if err := L.DoString(`require "json"`); err != nil {
348+
return err
349+
}
350+
L.Pop(L.GetTop())
344351

345352
// Register the MetaSpec TypeMetatable
346353
registerMetaSpecType(L)

lua_test.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ package main
22

33
import (
44
"bufio"
5+
"os"
6+
"os/exec"
7+
"strings"
8+
"testing"
9+
510
"github.com/stretchr/testify/assert"
611
"github.com/stretchr/testify/require"
712
"github.com/stretchr/testify/suite"
813
"github.com/vadv/gopher-lua-libs/tests"
914
lua "github.com/yuin/gopher-lua"
10-
"os"
11-
"os/exec"
12-
"strings"
13-
"testing"
1415
)
1516

1617
type LuaSuite struct {
@@ -64,6 +65,18 @@ func (s *LuaSuite) TestArg_Passing_Json() {
6465
s.Assert().NoError(s.LuaSpec.Do("testdata/test-arg-passing-json.lua", `{"foo": "bar", "bar": [1, 2, 3.45]}`))
6566
}
6667

68+
func (s *LuaSuite) TestSetEmptyObjectIsObject() {
69+
s.MetaSpec.JSONValue = true
70+
s.Require().NoError(s.MetaSpec.Set("foo", "{}"))
71+
s.MetaSpec.JSONValue = false
72+
73+
s.LuaSpec.EvaluateString = `meta.set("foo", meta.get("foo"))`
74+
s.Require().NoError(s.LuaSpec.Do())
75+
foo, err := s.MetaSpec.Get("foo")
76+
s.Require().NoError(err)
77+
s.Equal("{}", foo)
78+
}
79+
6780
func (s *LuaSuite) TestCLI() {
6881
type testCase struct {
6982
name string

0 commit comments

Comments
 (0)