1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
package linguist
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v15/internal/git"
"gitlab.com/gitlab-org/gitaly/v15/internal/git/catfile"
"gitlab.com/gitlab-org/gitaly/v15/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/v15/internal/git/localrepo"
"gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/v15/internal/metadata/featureflag"
"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg"
"gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb"
)
func TestMain(m *testing.M) {
testhelper.Run(m)
}
func TestInstance_Stats(t *testing.T) {
testhelper.NewFeatureSets(featureflag.GoLanguageStats).
Run(t, testInstanceStats)
}
func testInstanceStats(t *testing.T, ctx context.Context) {
cfg := testcfg.Build(t)
gitCmdFactory := gittest.NewCommandFactory(t, cfg)
linguist, err := New(cfg, gitCmdFactory)
require.NoError(t, err)
catfileCache := catfile.NewCache(cfg)
t.Cleanup(catfileCache.Stop)
commitID := git.ObjectID("1e292f8fedd741b75372e19097c76d327140c312")
languageStatsFilename := filenameForCache(ctx)
for _, tc := range []struct {
desc string
setup func(t *testing.T) (*gitalypb.Repository, string, git.ObjectID)
expectedStats ByteCountPerLanguage
expectedErr string
}{
{
desc: "successful",
setup: func(t *testing.T) (*gitalypb.Repository, string, git.ObjectID) {
repoProto, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0])
return repoProto, repoPath, commitID
},
expectedStats: map[string]uint64{
"CoffeeScript": 107,
"HTML": 349,
"JavaScript": 1014,
"Ruby": 2943,
},
},
{
desc: "preexisting cache",
setup: func(t *testing.T) (*gitalypb.Repository, string, git.ObjectID) {
repoProto, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0])
repo := localrepo.NewTestRepo(t, cfg, repoProto)
// We simply run the linguist once before so that it can already
// write the cache.
_, err := linguist.Stats(ctx, repo, commitID.String(), catfileCache)
require.NoError(t, err)
require.FileExists(t, filepath.Join(repoPath, languageStatsFilename))
// Make sure it isn't able to generate stats from scratch
require.NoError(t, os.RemoveAll(filepath.Join(repoPath, "objects", "pack")))
return repoProto, repoPath, commitID
},
expectedStats: map[string]uint64{
"CoffeeScript": 107,
"HTML": 349,
"JavaScript": 1014,
"Ruby": 2943,
},
},
{
desc: "corrupted cache",
setup: func(t *testing.T) (*gitalypb.Repository, string, git.ObjectID) {
repoProto, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0])
require.NoError(t, os.WriteFile(filepath.Join(repoPath, languageStatsFilename), []byte("garbage"), 0o644))
return repoProto, repoPath, commitID
},
expectedStats: map[string]uint64{
"CoffeeScript": 107,
"HTML": 349,
"JavaScript": 1014,
"Ruby": 2943,
},
},
{
desc: "old cache",
setup: func(t *testing.T) (*gitalypb.Repository, string, git.ObjectID) {
repoProto, repoPath := gittest.InitRepo(t, cfg, cfg.Storages[0])
repo := localrepo.NewTestRepo(t, cfg, repoProto)
oldCommitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(), gittest.WithTreeEntries(
gittest.TreeEntry{Path: "main.rb", Content: "require 'fileutils'", Mode: "100644"},
))
newCommitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(oldCommitID), gittest.WithTreeEntries(
gittest.TreeEntry{Path: "main.go", Content: "package main", Mode: "100644"},
))
// Precreate the cache with the old commit. This ensures that
// linguist knows to update the cache.
stats, err := linguist.Stats(ctx, repo, oldCommitID.String(), catfileCache)
require.NoError(t, err)
require.FileExists(t, filepath.Join(repoPath, languageStatsFilename))
require.Equal(t, ByteCountPerLanguage{
"Ruby": 19,
}, stats)
return repoProto, repoPath, newCommitID
},
expectedStats: map[string]uint64{
"Go": 12,
},
},
{
desc: "missing repository",
setup: func(t *testing.T) (*gitalypb.Repository, string, git.ObjectID) {
repoPath := filepath.Join(testhelper.TempDir(t), "nonexistent")
repoProto := &gitalypb.Repository{StorageName: cfg.Storages[0].Name, RelativePath: "nonexistent"}
return repoProto, repoPath, commitID
},
expectedErr: "GetRepoPath: not a git repository",
},
{
desc: "missing commit",
setup: func(t *testing.T) (*gitalypb.Repository, string, git.ObjectID) {
repoProto, repoPath := gittest.InitRepo(t, cfg, cfg.Storages[0])
return repoProto, repoPath, commitID
},
expectedErr: "linguist",
},
} {
t.Run(tc.desc, func(t *testing.T) {
repoProto, repoPath, objectID := tc.setup(t)
repo := localrepo.NewTestRepo(t, cfg, repoProto)
stats, err := linguist.Stats(ctx, repo, objectID.String(), catfileCache)
if tc.expectedErr == "" {
require.NoError(t, err)
require.Equal(t, tc.expectedStats, stats)
require.FileExists(t, filepath.Join(repoPath, languageStatsFilename))
} else {
require.Contains(t, err.Error(), tc.expectedErr)
}
})
}
}
func TestInstance_Stats_unmarshalJSONError(t *testing.T) {
cfg := testcfg.Build(t)
ctx := featureflag.ContextWithFeatureFlag(testhelper.Context(t), featureflag.GoLanguageStats, false)
gitCmdFactory := gittest.NewCommandFactory(t, cfg)
invalidRepo := &gitalypb.Repository{StorageName: "fake", RelativePath: "path"}
catfileCache := catfile.NewCache(cfg)
t.Cleanup(catfileCache.Stop)
repo := localrepo.New(config.NewLocator(cfg), gitCmdFactory, catfileCache, invalidRepo)
ling, err := New(cfg, gitCmdFactory)
require.NoError(t, err)
// When an error occurs, this used to trigger JSON marshelling of a plain string
// the new behaviour shouldn't do that, and return an command error
_, err = ling.Stats(ctx, repo, "deadbeef", catfileCache)
require.Error(t, err)
_, ok := err.(*json.SyntaxError)
require.False(t, ok, "expected the error not be a json Syntax Error")
}
func TestInstance_Stats_incremental(t *testing.T) {
t.Parallel()
cfg := testcfg.Build(t)
logger, hook := test.NewNullLogger()
ctx := testhelper.Context(t, testhelper.ContextWithLogger(logrus.NewEntry(logger)))
ctx = featureflag.ContextWithFeatureFlag(ctx, featureflag.GoLanguageStats, true)
gitCmdFactory := gittest.NewCommandFactory(t, cfg)
linguist, err := New(cfg, gitCmdFactory)
require.NoError(t, err)
catfileCache := catfile.NewCache(cfg)
t.Cleanup(catfileCache.Stop)
repoProto, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0])
repo := localrepo.NewTestRepo(t, cfg, repoProto)
cleanStats, err := linguist.Stats(ctx, repo, "1e292f8fedd741b75372e19097c76d327140c312", catfileCache)
require.NoError(t, err)
require.Len(t, hook.AllEntries(), 0)
require.NoError(t, os.Remove(filepath.Join(repoPath, languageStatsFilename)))
_, err = linguist.Stats(ctx, repo, "cfe32cf61b73a0d5e9f13e774abde7ff789b1660", catfileCache)
require.NoError(t, err)
require.Len(t, hook.AllEntries(), 0)
require.FileExists(t, filepath.Join(repoPath, languageStatsFilename))
incStats, err := linguist.Stats(ctx, repo, "1e292f8fedd741b75372e19097c76d327140c312", catfileCache)
require.NoError(t, err)
require.Len(t, hook.AllEntries(), 0)
require.FileExists(t, filepath.Join(repoPath, languageStatsFilename))
require.Equal(t, cleanStats, incStats)
}
func TestNew(t *testing.T) {
cfg := testcfg.Build(t, testcfg.WithRealLinguist())
ling, err := New(cfg, gittest.NewCommandFactory(t, cfg))
require.NoError(t, err)
require.Equal(t, "#701516", ling.Color("Ruby"), "color value for 'Ruby'")
}
func TestNew_loadLanguagesCustomPath(t *testing.T) {
jsonPath, err := filepath.Abs("testdata/fake-languages.json")
require.NoError(t, err)
cfg := testcfg.Build(t, testcfg.WithBase(config.Cfg{Ruby: config.Ruby{LinguistLanguagesPath: jsonPath}}))
ling, err := New(cfg, gittest.NewCommandFactory(t, cfg))
require.NoError(t, err)
require.Equal(t, "foo color", ling.Color("FooBar"))
}
// filenameForCache returns the filename where the cache is stored, depending on
// the feature flag.
func filenameForCache(ctx context.Context) string {
if featureflag.GoLanguageStats.IsDisabled(ctx) {
return "language-stats.cache"
}
return languageStatsFilename
}
func BenchmarkInstance_Stats(b *testing.B) {
testhelper.NewFeatureSets(featureflag.GoLanguageStats).
Bench(b, benchmarkInstanceStats)
}
func benchmarkInstanceStats(b *testing.B, ctx context.Context) {
cfg := testcfg.Build(b)
gitCmdFactory := gittest.NewCommandFactory(b, cfg)
languageStatsFilename := filenameForCache(ctx)
linguist, err := New(cfg, gitCmdFactory)
require.NoError(b, err)
catfileCache := catfile.NewCache(cfg)
b.Cleanup(catfileCache.Stop)
repoProto, repoPath := gittest.CloneRepo(b, cfg, cfg.Storages[0], gittest.CloneRepoOpts{
SourceRepo: "benchmark.git",
})
repo := localrepo.NewTestRepo(b, cfg, repoProto)
var scratchStat ByteCountPerLanguage
var incStats ByteCountPerLanguage
b.Run("from scratch", func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
require.NoError(b, os.RemoveAll(filepath.Join(repoPath, languageStatsFilename)))
b.StartTimer()
scratchStat, err = linguist.Stats(ctx, repo, "f5dfdd0057cd6bffc6259a5c8533dde5bf6a9d37", catfileCache)
require.NoError(b, err)
}
})
b.Run("incremental", func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
require.NoError(b, os.RemoveAll(filepath.Join(repoPath, languageStatsFilename)))
// a commit about 3 months older than the next
_, err = linguist.Stats(ctx, repo, "3c813b292d25a9b2ffda70e7f609f623bfc0cb37", catfileCache)
b.StartTimer()
incStats, err = linguist.Stats(ctx, repo, "f5dfdd0057cd6bffc6259a5c8533dde5bf6a9d37", catfileCache)
require.NoError(b, err)
}
})
require.Equal(b, scratchStat, incStats)
}
|