| 1 |
package git |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/stretchr/testify/assert" |
| 7 |
"github.com/stretchr/testify/require" |
| 8 |
|
| 9 |
"github.com/go-git/go-git/v5/config" |
| 10 |
formatcfg "github.com/go-git/go-git/v5/plumbing/format/config" |
| 11 |
"github.com/go-git/go-git/v5/storage/memory" |
| 12 |
) |
| 13 |
|
| 14 |
func TestVerifyExtensions(t *testing.T) { |
| 15 |
t.Parallel() |
| 16 |
|
| 17 |
tests := []struct { |
| 18 |
name string |
| 19 |
setup func(*testing.T, *config.Config) |
| 20 |
wantErr string |
| 21 |
}{ |
| 22 |
{ |
| 23 |
name: "repositoryformatversion=0: invalid extension", |
| 24 |
setup: func(t *testing.T, cfg *config.Config) { |
| 25 |
cfg.Core.RepositoryFormatVersion = formatcfg.Version_0 |
| 26 |
cfg.Raw.Section("extensions").SetOption("unknown", "foo") |
| 27 |
cfg.Raw.Section("extensions").SetOption("objectformat", "sha1") |
| 28 |
}, |
| 29 |
wantErr: "repositoryformatversion does not support extension: unknown, objectformat", |
| 30 |
}, |
| 31 |
{ |
| 32 |
name: "repositoryformatversion=0: allows supported noop", |
| 33 |
setup: func(t *testing.T, cfg *config.Config) { |
| 34 |
cfg.Core.RepositoryFormatVersion = formatcfg.Version_0 |
| 35 |
cfg.Raw.Section("extensions").SetOption("noop", "bar") |
| 36 |
}, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "repositoryformatversion='': allows supported noop", |
| 40 |
setup: func(t *testing.T, cfg *config.Config) { |
| 41 |
cfg.Raw.Section("extensions").SetOption("noop", "bar") |
| 42 |
}, |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "repositoryformatversion=1: rejects unknown extensions", |
| 46 |
setup: func(t *testing.T, cfg *config.Config) { |
| 47 |
cfg.Core.RepositoryFormatVersion = formatcfg.Version_1 |
| 48 |
cfg.Raw.Section("extensions").SetOption("unknownext", "true") |
| 49 |
}, |
| 50 |
wantErr: "unknown extension: unknownext", |
| 51 |
}, |
| 52 |
{ |
| 53 |
name: "repositoryformatversion=1: allows known extension", |
| 54 |
setup: func(t *testing.T, cfg *config.Config) { |
| 55 |
cfg.Core.RepositoryFormatVersion = formatcfg.Version_1 |
| 56 |
cfg.Raw.Section("extensions").SetOption("NOOP", "foo") |
| 57 |
cfg.Raw.Section("extensions").SetOption("noop-v1", "bar") |
| 58 |
}, |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "repositoryformatversion=1: rejects objectformat=sha1", // not supported in go-git/v5 |
| 62 |
setup: func(t *testing.T, cfg *config.Config) { |
| 63 |
cfg.Core.RepositoryFormatVersion = formatcfg.Version_1 |
| 64 |
cfg.Raw.Section("extensions").SetOption("objectformat", "sha1") |
| 65 |
}, |
| 66 |
wantErr: "unknown extension: objectformat", |
| 67 |
}, |
| 68 |
} |
| 69 |
|
| 70 |
for _, tt := range tests { |
| 71 |
t.Run(tt.name, func(t *testing.T) { |
| 72 |
t.Parallel() |
| 73 |
|
| 74 |
st := memory.NewStorage() |
| 75 |
|
| 76 |
r, err := Init(st, nil) |
| 77 |
require.NoError(t, err) |
| 78 |
require.NotNil(t, r) |
| 79 |
|
| 80 |
cfg, err := st.Config() |
| 81 |
require.NoError(t, err) |
| 82 |
|
| 83 |
tt.setup(t, cfg) |
| 84 |
require.NoError(t, st.SetConfig(cfg)) |
| 85 |
|
| 86 |
r, err = Open(st, nil) |
| 87 |
if tt.wantErr != "" { |
| 88 |
require.Error(t, err) |
| 89 |
assert.Contains(t, err.Error(), tt.wantErr) |
| 90 |
assert.Nil(t, r) |
| 91 |
} else { |
| 92 |
require.NoError(t, err) |
| 93 |
assert.NotNil(t, r) |
| 94 |
} |
| 95 |
}) |
| 96 |
} |
| 97 |
} |