this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

encoding/gocode: be resilient to Go string literal changes

The way that Go encodes string literals changes between
go1.18 and go1.19, which breaks this test.
We care about the actual bytes because the tests in `gen_test.go`
perform functional tests on the generated code (rather than
generating to a temporary location and testing that), so it's
important to know that those tests are running on that exact code.

So we normalize the code before comparison to make the test
resilient to both encoding conventions.

Also fix the tests in `gen_test.go` and update the generated files
to correspond with the new quoting convention.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I5ac51093e45933f6a4ae28eacad36d45c1ddc1e9
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/542972
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+75 -48
+63 -45
encoding/gocode/gen_test.go
··· 25 25 "cuelang.org/go/encoding/gocode/testdata/pkg2" 26 26 ) 27 27 28 + type validator interface { 29 + Validate() error 30 + } 31 + 28 32 func TestPackages(t *testing.T) { 29 - t.Skip("fix error messages") // TODO(errors) 30 33 31 34 testCases := []struct { 32 - name string 33 - got error 34 - want string 35 + name string 36 + value validator 37 + want string 35 38 }{{ 36 - name: "failing int", 37 - got: func() error { 38 - v := pkg2.PickMe(4) 39 - return v.Validate() 40 - }(), 41 - want: "invalid value 4 (out of bound >5):\n pkg2/instance.cue:x:x", 39 + name: "failing int", 40 + value: pkg2.PickMe(4), 41 + want: "invalid value 4 (out of bound >5):\n pkg2/instance.cue:x:x", 42 42 }, { 43 - name: "failing field with validator", 44 - got: func() error { 45 - v := &pkg1.OtherStruct{A: "car"} 46 - return v.Validate() 47 - }(), 48 - want: "A: invalid value \"car\" (does not satisfy strings.ContainsAny(\"X\")):\n pkg1/instance.cue:x:x\nP: invalid value 0 (out of bound >5):\n pkg2/instance.cue:x:x", 43 + name: "failing field with validator", 44 + value: &pkg1.OtherStruct{A: "car"}, 45 + want: ` 46 + 2 errors in empty disjunction: 47 + conflicting values null and {A:strings.ContainsAny("X"),P:"cuelang.org/go/encoding/gocode/testdata/pkg2".PickMe} (mismatched types null and struct): 48 + pkg1/instance.cue:x:x 49 + A: invalid value "car" (does not satisfy strings.ContainsAny("X")): 50 + pkg1/instance.cue:x:x 51 + pkg1/instance.cue:x:x 52 + `, 49 53 }, { 50 - name: "failing field of type int", 51 - got: func() error { 52 - v := &pkg1.MyStruct{A: 11, B: "dog"} 53 - return v.Validate() 54 - }(), 55 - want: "A: invalid value 11 (out of bound <=10):\n pkg1/instance.cue:x:x", 54 + name: "failing field of type int", 55 + value: &pkg1.MyStruct{A: 11, B: "dog"}, 56 + want: ` 57 + 2 errors in empty disjunction: 58 + conflicting values null and {A:<=10,B:(=~"cat"|*"dog"),O?:OtherStruct,I:"cuelang.org/go/encoding/gocode/testdata/pkg2".ImportMe} (mismatched types null and struct): 59 + pkg1/instance.cue:x:x 60 + A: invalid value 11 (out of bound <=10): 61 + pkg1/instance.cue:x:x 62 + `, 56 63 }, { 57 - name: "failing nested struct ", 58 - got: func() error { 59 - v := &pkg1.MyStruct{A: 5, B: "dog", O: &pkg1.OtherStruct{A: "car", P: 6}} 60 - return v.Validate() 61 - }(), 62 - want: "O.A: invalid value \"car\" (does not satisfy strings.ContainsAny(\"X\")):\n pkg1/instance.cue:x:x", 64 + name: "failing nested struct ", 65 + value: &pkg1.MyStruct{A: 5, B: "dog", O: &pkg1.OtherStruct{A: "car", P: 6}}, 66 + want: ` 67 + 4 errors in empty disjunction: 68 + conflicting values null and {A:<=10,B:(=~"cat"|*"dog"),O?:OtherStruct,I:"cuelang.org/go/encoding/gocode/testdata/pkg2".ImportMe} (mismatched types null and struct): 69 + pkg1/instance.cue:x:x 70 + O: 2 errors in empty disjunction: 71 + O: conflicting values null and {A:strings.ContainsAny("X"),P:"cuelang.org/go/encoding/gocode/testdata/pkg2".PickMe} (mismatched types null and struct): 72 + pkg1/instance.cue:x:x 73 + pkg1/instance.cue:x:x 74 + O.A: invalid value "car" (does not satisfy strings.ContainsAny("X")): 75 + pkg1/instance.cue:x:x 76 + pkg1/instance.cue:x:x 77 + `, 63 78 }, { 64 - name: "fail nested struct of different package", 65 - got: func() error { 66 - v := &pkg1.MyStruct{A: 5, B: "dog", O: &pkg1.OtherStruct{A: "X", P: 4}} 67 - return v.Validate() 68 - }(), 69 - want: "O.P: invalid value 4 (out of bound >5):\n pkg2/instance.cue:x:x", 79 + name: "fail nested struct of different package", 80 + value: &pkg1.MyStruct{A: 5, B: "dog", O: &pkg1.OtherStruct{A: "X", P: 4}}, 81 + want: ` 82 + 4 errors in empty disjunction: 83 + conflicting values null and {A:<=10,B:(=~"cat"|*"dog"),O?:OtherStruct,I:"cuelang.org/go/encoding/gocode/testdata/pkg2".ImportMe} (mismatched types null and struct): 84 + pkg1/instance.cue:x:x 85 + O: 2 errors in empty disjunction: 86 + O: conflicting values null and {A:strings.ContainsAny("X"),P:"cuelang.org/go/encoding/gocode/testdata/pkg2".PickMe} (mismatched types null and struct): 87 + pkg1/instance.cue:x:x 88 + pkg1/instance.cue:x:x 89 + O.P: invalid value 4 (out of bound >5): 90 + pkg2/instance.cue:x:x 91 + `, 70 92 }, { 71 93 name: "all good", 72 - got: func() error { 73 - v := &pkg1.MyStruct{ 74 - A: 5, 75 - B: "dog", 76 - I: &pkg2.ImportMe{A: 1000, B: "a"}, 77 - } 78 - return v.Validate() 79 - }(), 94 + value: &pkg1.MyStruct{ 95 + A: 5, 96 + B: "dog", 97 + I: &pkg2.ImportMe{A: 1000, B: "a"}, 98 + }, 80 99 want: "nil", 81 100 }} 82 101 for _, tc := range testCases { 83 102 t.Run(tc.name, func(t *testing.T) { 84 - got := strings.TrimSpace(errStr(tc.got)) 85 - want := tc.want 103 + got := strings.TrimSpace(errStr(tc.value.Validate())) 104 + want := strings.TrimSpace(tc.want) 86 105 if got != want { 87 106 t.Errorf("got:\n%q\nwant:\n%q", got, want) 88 107 } 89 - 90 108 }) 91 109 } 92 110 }
+10 -1
encoding/gocode/generator_test.go
··· 74 74 if err != nil { 75 75 t.Fatal(err) 76 76 } 77 + want, b = normalize(want), normalize(b) 77 78 78 79 if d := diff.Diff(string(want), string(b)); d != "" { 79 - t.Errorf("files differ:\n%v", d) 80 + t.Errorf("files differ (-want +got):\n%v", d) 80 81 } 81 82 }) 82 83 } 84 + } 85 + 86 + // normalize copes with differences between the Go string literal conventions 87 + // between go1.18 and go1.19. The byte 0x7f changed from \u007f to \x7f. 88 + // By normalizing here, we make the comparison independent of the Go version 89 + // that's used to run the tests. 90 + func normalize(gen []byte) []byte { 91 + return bytes.ReplaceAll(gen, []byte(`\u007f`), []byte(`\x7f`)) 83 92 } 84 93 85 94 func errStr(err error) string {
+1 -1
encoding/gocode/testdata/pkg1/cue_gen.go
··· 82 82 } 83 83 84 84 // Data size: 601 bytes. 85 - var cuegenInstanceData = []byte("\x01\x1f\x8b\b\x00\x00\x00\x00\x00\x00\xff\x94R\xdfo\xd3:\x18\xb5\xd3^\xe9\xd6\u06bd\x12\u007f\x00\u0487\x9fZ4\xd2\x1f\x12B\x8a\x16`\x1b\x03\xedaka\x80\x10\x88\a/\xf9\x9aZs\xed\x908c\x15l\x120\xc6\xfe\xea\x059M\xb6\xc2\xdb\xf2\x92\xa3/9\xdf9>>\xff\x95\xbf<\ua557\x84\x96\xdf\tyT~kQ\xba&un\x85\x8e\xf0\x99\xb0\xc2\xcdi\x8b\xb6_\x19c\xa9Gh{\"\uc32e\x11\xfa\xcfs\xa90\xa7\xe5\x05!\xe4n\xf9\u04e3\xf4\xff\x0f\x1f\xa3\x02\xfd\xa9T5\xf3\x82\xd0\xf2\x9c\x90n\xf9\xa3E\xe9\xbf7\xf3sB=\xda\xde\x17st\x8b\xda\u0550\x11B\xaeZ/\xcbK\xe2QJ\u05e3\x02\x95\u0409o\xb2\xa4\x9f\x98>\xea\xc8\xc4R;\x1c\x99\x18\xfb\x16s\x1b\v+\xfa\xe9Q2\xa4\x94\xdeq\xef~\xe3\u06cf\n\xa4W\xde\xfbTDG\"Ap\x1f\x19\x93\xf3\xd4d\x16\xba\xac\xc3o\xb1}\xc4Y\x87\xe76\x93:\xc9\x1d\x9c\v;\xe3\xac\xc7\xd8\xde\xe2\xc0fEd\x03\xf8\xc2:\x9b\x01\xc0F8\x1c\xb0\xceV\x00\x10\x9e\xf1HX\x0e_\xe1>\x8fM\xc2Yg\xfc$\x80\xb1\x9da\xb6\xe4\xb0\xcen\x00\xce\xd6\xc8\u07ed\\\xed!;\x85\xa7\x89\xe9\xaeGf\x9e*\xb4\x18n\u05e0\xc7V\x88\x8dXm\xc8\xdf6\xda\n\xa9\xf3M\xbd\xe8\xf2w\xbc\xc7:\x93`\xb9w\"\xa3#\xb7\x95\x1dT\xbf\x06P?\xf7B\xce\x1b\\\t\x1e\v%ca1|[\x83\xed7;=v\x90b$\x85j\xc8\xe1\x19\u03d7\x13\xbed\xd9E\x8a\xe1\xd2E\x8f\xed&\xdad\xf8z&\xf3J&<\xe3Sc8\x1b\u03e5\xbd\xd6\x05\x90\xdaV\xdc\a=\xc6\xfa}\xd87z\xe7D\xe6V\xea\x04>K\xa5\xe0\x10\xc1\u0325\xb5\x18\x83\xc8\xc1\x9d\x19A\xe6\xa0\r\xe0\xa7B\x1e\v\x85\xda\xc2\v\x03N\xdag+\xf4*\x94\xad&\x94:\xc8ZEV\u05a0\xd0x\xe2r\xc6\x18\n\xad0\xcf\x01OR%#i\xd5\x02P\x8bC\x85\xb1\u03e6\xc6\x04\xce&\x9b\u062cI\xda\u0777\xbfW(+S\x85\xe3iw8\xe8\xb1SF\x88w\x9b\x8a\x8e\ua28e\xfe\xac\xa8X)\xe8\u8ea07mcM9\x1a3\x1b\xc3\xc1`\xe5\xa8\u007f\u077f8\x8c\xb83\xb7\xbc\xfa\x00\x1e?d\x84\xfc\x0e\x00\x00\xff\xff\xdb\r\u0320\xe1\x03\x00\x00") 85 + var cuegenInstanceData = []byte("\x01\x1f\x8b\b\x00\x00\x00\x00\x00\x00\xff\x94R\xdfo\x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xf3J&<\xe3Sc8\x1b\u03e5\xbd\xd6\x05\x90\xdaV\xdc\a=\xc6\xfa}\xd87z\xe7D\xe6V\xea\x04>K\xa5\xe0\x10\xc1\u0325\xb5\x18\x83\xc8\xc1\x9d\x19A\xe6\xa0\r\xe0\xa7B\x1e\v\x85\xda\xc2\v\x03N\xdag+\xf4*\x94\xad&\x94:\xc8ZEV\u05a0\xd0x\xe2r\xc6\x18\n\xad0\xcf\x01OR%#i\xd5\x02P\x8bC\x85\xb1\u03e6\xc6\x04\xce&\x9b\u062cI\xda\u0777\xbfW(+S\x85\xe3iw8\xe8\xb1SF\x88w\x9b\x8a\x8e\ua28e\xfe\xac\xa8X)\xe8\u8ea07mcM9\x1a3\x1b\xc3\xc1`\xe5\xa8\x7f\u077f8\x8c\xb83\xb7\xbc\xfa\x00\x1e?d\x84\xfc\x0e\x00\x00\xff\xff\xdb\r\u0320\xe1\x03\x00\x00")
+1 -1
encoding/gocode/testdata/pkg2/cue_gen.go
··· 56 56 } 57 57 58 58 // Data size: 276 bytes. 59 - var cuegenInstanceData = []byte("\x01\x1f\x8b\b\x00\x00\x00\x00\x00\x00\xffD\x8e\xc1J\x031\x10\x86\xe7\u07ee`C\x15|\x00a\xd9S\x05\u066d\x82\bE\x84\xaa\b\x1e\x94\xe2U<\x8ci\x8c\xa1mR\x9a\xecA\u0103Z\xabO\xe3+FR*\x9ef\xf8f\xfe\x9fo+~e\xc8\xe27!\xbe\x13\x1d\u01f7\x16\xd01\xd6\a\xb6R]p\xe0\xc4\xd1B~\xeb\\@F\u0207\x1c\x9e\xd0!l\\\x9a\x89\xf2\x88K\"\u068d\x9f\x19\xb0}w/\x1bU=\x9a\xc9:\xb9$\xc4\x05Q7~\xb4\x80\xcd\u007f\xbe d\xc8ox\xaaRQ\xbe\x82\x82\x88\xe2O\x12\x01\xb0/\x1b5a\xab+7\u05f5v\xb5\xb2\u048d\x8cM\xbbt#U\a\xe5\u00c8\x03\u05f3\xb1>\x04\xb0\x93f\xfd\xa7]\xc9F\x81g,\u01ecU\x91NB\x98\xe9\xcc\xcdCQ\xfa07V\xfbR\x88\xab\x15\xb9V\xfd\xe2E\xb4\a\xfd\xe2\xe4\xa0\xd7\x13\xed\xb3~\xb1~\xa9\u039d\rl\xac\x1f\xd8\xe7n\xc9\x0f\xb2\xdc\x13\xafbh\xe48eN\x8f\x04\xd1o\x00\x00\x00\xff\xff\ue135\t=\x01\x00\x00") 59 + var cuegenInstanceData = []byte("\x01\x1f\x8b\b\x00\x00\x00\x00\x00\x00\xffD\x8e\xc1J\x031\x10\x86\xe7\u07ee`C\x15|\x00a\xd9S\x05\u066d\x82\bE\x84\xaa\b\x1e\x94\xe2U<\x8ci\x8c\xa1mR\x9a\xecA\u0103Z\xabO\xe3+FR*\x9ef\xf8f\xfe\x9fo+~e\xc8\xe27!\xbe\x13\x1d\u01f7\x16\xd01\xd6\a\xb6R]p\xe0\xc4\xd1B~\xeb\\@F\u0207\x1c\x9e\xd0!l\\\x9a\x89\xf2\x88K\"\u068d\x9f\x19\xb0}w/\x1bU=\x9a\xc9:\xb9$\xc4\x05Q7~\xb4\x80\xcd\x7f\xbe d\xc8ox\xaaRQ\xbe\x82\x82\x88\xe2O\x12\x01\xb0/\x1b5a\xab+7\u05f5v\xb5\xb2\u048d\x8cM\xbbt#U\a\xe5\u00c8\x03\u05f3\xb1>\x04\xb0\x93f\xfd\xa7]\xc9F\x81g,\u01ecU\x91NB\x98\xe9\xcc\xcdCQ\xfa07V\xfbR\x88\xab\x15\xb9V\xfd\xe2E\xb4\a\xfd\xe2\xe4\xa0\xd7\x13\xed\xb3~\xb1~\xa9\u039d\rl\xac\x1f\xd8\xe7n\xc9\x0f\xb2\xdc\x13\xafbh\xe48eN\x8f\x04\xd1o\x00\x00\x00\xff\xff\ue135\t=\x01\x00\x00")