github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/tiltfile/encoding/json_test.go (about)

     1  package encoding
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/tilt-dev/tilt/internal/testutils"
     8  	"github.com/tilt-dev/tilt/internal/tiltfile/io"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestReadJSON(t *testing.T) {
    14  	f := newFixture(t)
    15  
    16  	f.UseRealFS()
    17  
    18  	var document = `{
    19  	  "key1": "foo",
    20  	  "key2": {
    21  	    "key3": "bar",
    22  	    "key4": true
    23  	  },
    24        "key5": 3
    25  	}
    26  	`
    27  	f.File("options.json", document)
    28  	f.File("Tiltfile", `
    29  result = read_json("options.json")
    30  
    31  expected = {
    32    'key1': 'foo',
    33    'key2': {
    34      'key3': 'bar',
    35      'key4': True
    36    },
    37    'key5': 3,
    38  }
    39  
    40  def test():
    41  	if expected != result:
    42  		print('expected: %s' % (expected))
    43  		print('observed: %s' % (result))
    44  		fail()
    45  
    46  test()
    47  `)
    48  
    49  	result, err := f.ExecFile("Tiltfile")
    50  	if err != nil {
    51  		fmt.Println(f.PrintOutput())
    52  	}
    53  	require.NoError(t, err)
    54  
    55  	rs, err := io.GetState(result)
    56  	require.NoError(t, err)
    57  	require.Contains(t, rs.Paths, f.JoinPath("options.json"))
    58  }
    59  
    60  func TestJSONDoesNotExist(t *testing.T) {
    61  	f := newFixture(t)
    62  
    63  	f.File("Tiltfile", `result = read_json("dne.json")`)
    64  	result, err := f.ExecFile("Tiltfile")
    65  	require.Error(t, err)
    66  	require.Contains(t, err.Error(), "dne.json")
    67  	testutils.AssertIsNotExist(t, err)
    68  
    69  	rs, err := io.GetState(result)
    70  	require.NoError(t, err)
    71  	require.Contains(t, rs.Paths, f.JoinPath("dne.json"))
    72  }
    73  
    74  func TestMalformedJSON(t *testing.T) {
    75  	f := newFixture(t)
    76  
    77  	f.UseRealFS()
    78  
    79  	f.File("options.json", `["foo", {"baz":["bar", "", 1, 2]}`)
    80  
    81  	f.File("Tiltfile", `result = read_json("options.json")`)
    82  	result, err := f.ExecFile("Tiltfile")
    83  	require.Error(t, err)
    84  	require.Contains(t, err.Error(), "error parsing JSON from")
    85  	require.Contains(t, err.Error(), "options.json: unexpected EOF")
    86  
    87  	rs, err := io.GetState(result)
    88  	require.NoError(t, err)
    89  	require.Contains(t, rs.Paths, f.JoinPath("options.json"))
    90  }
    91  
    92  func TestDecodeJSON(t *testing.T) {
    93  	f := newFixture(t)
    94  
    95  	for _, blob := range []bool{false, true} {
    96  		t.Run(fmt.Sprintf("blob: %v", blob), func(t *testing.T) {
    97  			d := `'["foo", {"baz":["bar", "", 1, 2]}]'`
    98  			if blob {
    99  				d = fmt.Sprintf("blob(%s)", d)
   100  			}
   101  			d = fmt.Sprintf("observed = decode_json(%s)", d)
   102  			tf := d + `
   103  expected = [
   104    "foo",
   105    {
   106      "baz": [ "bar", "", 1, 2],
   107    }
   108  ]
   109  
   110  def test():
   111  	if expected != observed:
   112  		print('expected: %s' % (expected))
   113  		print('observed: %s' % (observed))
   114  		fail()
   115  
   116  test()
   117  
   118  `
   119  			f.File("Tiltfile", tf)
   120  
   121  			_, err := f.ExecFile("Tiltfile")
   122  			if err != nil {
   123  				fmt.Println(f.PrintOutput())
   124  			}
   125  			require.NoError(t, err)
   126  		})
   127  	}
   128  }
   129  
   130  func TestEncodeJSON(t *testing.T) {
   131  	f := newFixture(t)
   132  
   133  	f.File("Tiltfile", `
   134  expected = '''[
   135    "foo",
   136    {
   137      "baz": [
   138        "bar",
   139        "",
   140        1,
   141        2
   142      ]
   143    }
   144  ]
   145  '''
   146  observed = encode_json([
   147    "foo",
   148    {
   149      "baz": [ "bar", "", 1, 2],
   150    }
   151  ])
   152  
   153  def test():
   154  	if expected != observed:
   155  		print('expected: %s' % (expected))
   156  		print('observed: %s' % (observed))
   157  		fail()
   158  
   159  test()
   160  
   161  `)
   162  
   163  	_, err := f.ExecFile("Tiltfile")
   164  	if err != nil {
   165  		fmt.Println(f.PrintOutput())
   166  	}
   167  	require.NoError(t, err)
   168  }
   169  
   170  func TestEncodeJSONNonStringMapKey(t *testing.T) {
   171  	f := newFixture(t)
   172  
   173  	f.File("Tiltfile", `encode_json({1: 'hello'})`)
   174  
   175  	_, err := f.ExecFile("Tiltfile")
   176  	require.Error(t, err)
   177  	require.Contains(t, err.Error(), "only string keys are supported in maps. found key '1' of type int64")
   178  }
   179  
   180  func TestEncodeJSONNonJSONable(t *testing.T) {
   181  	f := newFixture(t)
   182  
   183  	f.File("Tiltfile", `
   184  encode_json(blob('hello'))
   185  `)
   186  
   187  	_, err := f.ExecFile("Tiltfile")
   188  	require.Error(t, err)
   189  	require.Contains(t, err.Error(), "unsupported type io.Blob")
   190  }
   191  
   192  func TestDecodeJSONIntFloat(t *testing.T) {
   193  	f := newFixture(t)
   194  	f.File("Tiltfile", `
   195  json = '{"int":42,"float":3.14,"intfloat":3.0}'
   196  x = decode_json(json)
   197  if repr(x["int"]) != "42":
   198    fail('repr(int) value was not "42": ' + repr(x["int"]))
   199  if repr(x["float"]) != "3.14":
   200    fail('repr(float) value was not a "3.14": ' + repr(x["float"]))
   201  if repr(x["intfloat"]) != "3.0":
   202    fail('repr(intfloat) value was not a "3.0": ' + repr(x["intfloat"]))
   203  `)
   204  
   205  	_, err := f.ExecFile("Tiltfile")
   206  	if err != nil {
   207  		fmt.Println(f.PrintOutput())
   208  	}
   209  	require.NoError(t, err)
   210  }
   211  
   212  func TestDecodeInvalidJSONMultipleValues(t *testing.T) {
   213  	f := newFixture(t)
   214  	f.File("stream.json", `{"a":1,"b":2}
   215  {"a":2,"b":3}`)
   216  	f.File("Tiltfile", `read_json("stream.json")`)
   217  	_, err := f.ExecFile("Tiltfile")
   218  	require.Error(t, err)
   219  	require.Contains(t, err.Error(), "multiple JSON values")
   220  }