github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/scripts/confix/confix_test.go (about) 1 package main_test 2 3 import ( 4 "bytes" 5 "context" 6 "strings" 7 "testing" 8 9 "github.com/creachadair/tomledit" 10 "github.com/google/go-cmp/cmp" 11 12 confix "github.com/ari-anchor/sei-tendermint/scripts/confix" 13 ) 14 15 func mustParseConfig(t *testing.T, path string) *tomledit.Document { 16 doc, err := confix.LoadConfig(path) 17 if err != nil { 18 t.Fatalf("Loading config: %v", err) 19 } 20 return doc 21 } 22 23 func TestGuessConfigVersion(t *testing.T) { 24 tests := []struct { 25 path, want string 26 }{ 27 {"testdata/non-config.toml", ""}, 28 {"testdata/v30-config.toml", ""}, 29 {"testdata/v31-config.toml", ""}, 30 {"testdata/v32-config.toml", "v0.32"}, 31 {"testdata/v33-config.toml", "v0.33"}, 32 {"testdata/v34-config.toml", "v0.34"}, 33 {"testdata/v35-config.toml", "v0.35"}, 34 {"testdata/v36-config.toml", "v0.36"}, 35 } 36 for _, test := range tests { 37 t.Run(test.path, func(t *testing.T) { 38 got := confix.GuessConfigVersion(mustParseConfig(t, test.path)) 39 if got != test.want { 40 t.Errorf("Wrong version: got %q, want %q", got, test.want) 41 } 42 }) 43 } 44 } 45 46 func TestApplyFixes(t *testing.T) { 47 ctx := context.Background() 48 49 t.Run("Unknown", func(t *testing.T) { 50 err := confix.ApplyFixes(ctx, mustParseConfig(t, "testdata/v31-config.toml")) 51 if err == nil || !strings.Contains(err.Error(), "cannot tell what Tendermint version") { 52 t.Error("ApplyFixes succeeded, but should have failed for an unknown version") 53 } 54 }) 55 t.Run("TooOld", func(t *testing.T) { 56 err := confix.ApplyFixes(ctx, mustParseConfig(t, "testdata/v33-config.toml")) 57 if err == nil || !strings.Contains(err.Error(), "unable to update version v0.33 config") { 58 t.Errorf("ApplyFixes: got %v, want version error", err) 59 } 60 }) 61 t.Run("OK", func(t *testing.T) { 62 doc := mustParseConfig(t, "testdata/v34-config.toml") 63 if err := confix.ApplyFixes(ctx, doc); err != nil { 64 t.Fatalf("ApplyFixes: unexpected error: %v", err) 65 } 66 67 t.Run("Fixpoint", func(t *testing.T) { 68 // Verify that reapplying fixes to the same config succeeds, and does not 69 // make any additional changes. 70 var before bytes.Buffer 71 if err := tomledit.Format(&before, doc); err != nil { 72 t.Fatalf("Formatting document: %v", err) 73 } 74 if err := confix.CheckValid(before.Bytes()); err != nil { 75 t.Fatalf("Validating output: %v", err) 76 } 77 want := before.String() 78 79 // Re-parse the output from the first round of transformations. 80 doc2, err := tomledit.Parse(&before) 81 if err != nil { 82 t.Fatalf("Parsing fixed output: %v", err) 83 } 84 if err := confix.ApplyFixes(ctx, doc2); err != nil { 85 t.Fatalf("ApplyFixes: unexpected error: %v", err) 86 } 87 88 var after bytes.Buffer 89 if err := tomledit.Format(&after, doc2); err != nil { 90 t.Fatalf("Formatting document: %v", err) 91 } 92 got := after.String() 93 94 if diff := cmp.Diff(want, got); diff != "" { 95 t.Errorf("Reapplied fixes changed something: (-want, +got)\n%s", diff) 96 } 97 }) 98 }) 99 }