loading up the forgejo repo on tangled to test page performance
at forgejo 125 lines 3.3 kB view raw
1// Copyright twenty-panda <twenty-panda@posteo.com> 2// SPDX-License-Identifier: MIT 3 4package pushoptions 5 6import ( 7 "fmt" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11) 12 13func TestEmpty(t *testing.T) { 14 options := New() 15 assert.True(t, options.Empty()) 16 options.Parse(fmt.Sprintf("%v", RepoPrivate)) 17 assert.False(t, options.Empty()) 18} 19 20func TestToAndFromMap(t *testing.T) { 21 options := New() 22 options.Parse(fmt.Sprintf("%v", RepoPrivate)) 23 actual := options.Map() 24 expected := map[string]string{string(RepoPrivate): "true"} 25 assert.Equal(t, expected, actual) 26 assert.Equal(t, expected, NewFromMap(&actual).Map()) 27} 28 29func TestChangeRepositorySettings(t *testing.T) { 30 options := New() 31 assert.False(t, options.ChangeRepoSettings()) 32 assert.True(t, options.Parse(fmt.Sprintf("%v=description", AgitDescription))) 33 assert.False(t, options.ChangeRepoSettings()) 34 35 options.Parse(fmt.Sprintf("%v", RepoPrivate)) 36 assert.True(t, options.ChangeRepoSettings()) 37 38 options = New() 39 options.Parse(fmt.Sprintf("%v", RepoTemplate)) 40 assert.True(t, options.ChangeRepoSettings()) 41} 42 43func TestParse(t *testing.T) { 44 t.Run("no key", func(t *testing.T) { 45 options := New() 46 47 val, ok := options.GetString(RepoPrivate) 48 assert.False(t, ok) 49 assert.Empty(t, val) 50 51 assert.True(t, options.GetBool(RepoPrivate, true)) 52 assert.False(t, options.GetBool(RepoPrivate, false)) 53 }) 54 55 t.Run("key=value", func(t *testing.T) { 56 options := New() 57 58 topic := "TOPIC" 59 assert.True(t, options.Parse(fmt.Sprintf("%v=%s", AgitTopic, topic))) 60 val, ok := options.GetString(AgitTopic) 61 assert.True(t, ok) 62 assert.Equal(t, topic, val) 63 }) 64 65 t.Run("key=true", func(t *testing.T) { 66 options := New() 67 68 assert.True(t, options.Parse(fmt.Sprintf("%v=true", RepoPrivate))) 69 assert.True(t, options.GetBool(RepoPrivate, false)) 70 assert.True(t, options.Parse(fmt.Sprintf("%v=TRUE", RepoTemplate))) 71 assert.True(t, options.GetBool(RepoTemplate, false)) 72 }) 73 74 t.Run("key=false", func(t *testing.T) { 75 options := New() 76 77 assert.True(t, options.Parse(fmt.Sprintf("%v=false", RepoPrivate))) 78 assert.False(t, options.GetBool(RepoPrivate, true)) 79 }) 80 81 t.Run("key", func(t *testing.T) { 82 options := New() 83 84 assert.True(t, options.Parse(fmt.Sprintf("%v", RepoPrivate))) 85 assert.True(t, options.GetBool(RepoPrivate, false)) 86 }) 87 88 t.Run("unknown keys are ignored", func(t *testing.T) { 89 options := New() 90 91 assert.True(t, options.Empty()) 92 assert.False(t, options.Parse("unknown=value")) 93 assert.True(t, options.Empty()) 94 }) 95} 96 97func TestReadEnv(t *testing.T) { 98 t.Setenv(envPrefix+"_0", fmt.Sprintf("%v=true", AgitForcePush)) 99 t.Setenv(envPrefix+"_1", fmt.Sprintf("%v", RepoPrivate)) 100 t.Setenv(envPrefix+"_2", fmt.Sprintf("%v=equal=in string", AgitTitle)) 101 t.Setenv(envPrefix+"_3", "not=valid") 102 t.Setenv(envPrefix+"_4", fmt.Sprintf("%v=description", AgitDescription)) 103 t.Setenv(EnvCount, "5") 104 105 options := New().ReadEnv() 106 107 assert.True(t, options.GetBool(AgitForcePush, false)) 108 assert.True(t, options.GetBool(RepoPrivate, false)) 109 assert.False(t, options.GetBool(RepoTemplate, false)) 110 111 { 112 val, ok := options.GetString(AgitTitle) 113 assert.True(t, ok) 114 assert.Equal(t, "equal=in string", val) 115 } 116 { 117 val, ok := options.GetString(AgitDescription) 118 assert.True(t, ok) 119 assert.Equal(t, "description", val) 120 } 121 { 122 _, ok := options.GetString(AgitTopic) 123 assert.False(t, ok) 124 } 125}