1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-24 08:57:03 -05:00
forgejo/modules/git/repo_attribute_test.go
forgejo-backport-action 9f80081795 [v7.0/forgejo] [REFACTOR] git attribute: test proper cancellation and unify nul-byte reader (#2939)
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/2906

Following #2763 (refactor of git check-attr)
and #2866 (wrong log.Error format in check-attr)

- refactors the `nul-byte` reader to be used in both the streaming and one-off cases.
- add test for some failure cases
- don't log the error returned by `cmd.Run`, but return it to the `CheckPath` caller (which can then decide what to do with it).

This should solve the following flaky `log.Error` (or at least move it to the caller, instead of being inside a random goroutine):

https://codeberg.org/forgejo/forgejo/actions/runs/9541/jobs/5#jobstep-7-839

> FATAL ERROR: log.Error has been called: 2024/03/28 14:30:33 ...it/repo_attribute.go:313:func2() [E] Unable to open checker for 3fa2f829675543ecfc16b2891aebe8bf0608a8f4. Error: failed to run attr-check. Error: exit status 128
        Stderr: fatal: this operation must be run in a work tree

Co-authored-by: oliverpool <git@olivier.pfad.fr>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2939
Reviewed-by: oliverpool <oliverpool@noreply.codeberg.org>
Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
2024-04-01 06:44:46 +00:00

346 lines
10 KiB
Go

// Copyright 2021 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package git
import (
"context"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"code.gitea.io/gitea/modules/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewCheckAttrStdoutReader(t *testing.T) {
t.Run("two_times", func(t *testing.T) {
read := newCheckAttrStdoutReader(strings.NewReader(
".gitignore\x00linguist-vendored\x00unspecified\x00"+
".gitignore\x00linguist-vendored\x00specified",
), 1)
// first read
attr, err := read()
assert.NoError(t, err)
assert.Equal(t, map[string]GitAttribute{
"linguist-vendored": GitAttribute("unspecified"),
}, attr)
// second read
attr, err = read()
assert.NoError(t, err)
assert.Equal(t, map[string]GitAttribute{
"linguist-vendored": GitAttribute("specified"),
}, attr)
})
t.Run("incomplete", func(t *testing.T) {
read := newCheckAttrStdoutReader(strings.NewReader(
"filename\x00linguist-vendored",
), 1)
_, err := read()
assert.Equal(t, io.ErrUnexpectedEOF, err)
})
t.Run("three_times", func(t *testing.T) {
read := newCheckAttrStdoutReader(strings.NewReader(
"shouldbe.vendor\x00linguist-vendored\x00set\x00"+
"shouldbe.vendor\x00linguist-generated\x00unspecified\x00"+
"shouldbe.vendor\x00linguist-language\x00unspecified\x00",
), 1)
// first read
attr, err := read()
assert.NoError(t, err)
assert.Equal(t, map[string]GitAttribute{
"linguist-vendored": GitAttribute("set"),
}, attr)
// second read
attr, err = read()
assert.NoError(t, err)
assert.Equal(t, map[string]GitAttribute{
"linguist-generated": GitAttribute("unspecified"),
}, attr)
// third read
attr, err = read()
assert.NoError(t, err)
assert.Equal(t, map[string]GitAttribute{
"linguist-language": GitAttribute("unspecified"),
}, attr)
})
}
func TestGitAttributeBareNonBare(t *testing.T) {
if !SupportCheckAttrOnBare {
t.Skip("git check-attr supported on bare repo starting with git 2.40")
}
repoPath := filepath.Join(testReposDir, "language_stats_repo")
gitRepo, err := openRepositoryWithDefaultContext(repoPath)
require.NoError(t, err)
defer gitRepo.Close()
for _, commitID := range []string{
"8fee858da5796dfb37704761701bb8e800ad9ef3",
"341fca5b5ea3de596dc483e54c2db28633cd2f97",
} {
bareStats, err := gitRepo.GitAttributes(commitID, "i-am-a-python.p", LinguistAttributes...)
assert.NoError(t, err)
defer test.MockVariableValue(&SupportCheckAttrOnBare, false)()
cloneStats, err := gitRepo.GitAttributes(commitID, "i-am-a-python.p", LinguistAttributes...)
assert.NoError(t, err)
assert.EqualValues(t, cloneStats, bareStats)
refStats := cloneStats
t.Run("GitAttributeChecker/"+commitID+"/SupportBare", func(t *testing.T) {
bareChecker, err := gitRepo.GitAttributeChecker(commitID, LinguistAttributes...)
assert.NoError(t, err)
defer bareChecker.Close()
bareStats, err := bareChecker.CheckPath("i-am-a-python.p")
assert.NoError(t, err)
assert.EqualValues(t, refStats, bareStats)
})
t.Run("GitAttributeChecker/"+commitID+"/NoBareSupport", func(t *testing.T) {
defer test.MockVariableValue(&SupportCheckAttrOnBare, false)()
cloneChecker, err := gitRepo.GitAttributeChecker(commitID, LinguistAttributes...)
assert.NoError(t, err)
defer cloneChecker.Close()
cloneStats, err := cloneChecker.CheckPath("i-am-a-python.p")
assert.NoError(t, err)
assert.EqualValues(t, refStats, cloneStats)
})
}
}
func TestGitAttributes(t *testing.T) {
repoPath := filepath.Join(testReposDir, "language_stats_repo")
gitRepo, err := openRepositoryWithDefaultContext(repoPath)
require.NoError(t, err)
defer gitRepo.Close()
attr, err := gitRepo.GitAttributes("8fee858da5796dfb37704761701bb8e800ad9ef3", "i-am-a-python.p", LinguistAttributes...)
assert.NoError(t, err)
assert.EqualValues(t, map[string]GitAttribute{
"gitlab-language": "unspecified",
"linguist-detectable": "unspecified",
"linguist-documentation": "unspecified",
"linguist-generated": "unspecified",
"linguist-language": "Python",
"linguist-vendored": "unspecified",
}, attr)
attr, err = gitRepo.GitAttributes("341fca5b5ea3de596dc483e54c2db28633cd2f97", "i-am-a-python.p", LinguistAttributes...)
assert.NoError(t, err)
assert.EqualValues(t, map[string]GitAttribute{
"gitlab-language": "unspecified",
"linguist-detectable": "unspecified",
"linguist-documentation": "unspecified",
"linguist-generated": "unspecified",
"linguist-language": "Cobra",
"linguist-vendored": "unspecified",
}, attr)
}
func TestGitAttributeFirst(t *testing.T) {
repoPath := filepath.Join(testReposDir, "language_stats_repo")
gitRepo, err := openRepositoryWithDefaultContext(repoPath)
require.NoError(t, err)
defer gitRepo.Close()
t.Run("first is specified", func(t *testing.T) {
language, err := gitRepo.GitAttributeFirst("8fee858da5796dfb37704761701bb8e800ad9ef3", "i-am-a-python.p", "linguist-language", "gitlab-language")
assert.NoError(t, err)
assert.Equal(t, "Python", language.String())
})
t.Run("second is specified", func(t *testing.T) {
language, err := gitRepo.GitAttributeFirst("8fee858da5796dfb37704761701bb8e800ad9ef3", "i-am-a-python.p", "gitlab-language", "linguist-language")
assert.NoError(t, err)
assert.Equal(t, "Python", language.String())
})
t.Run("none is specified", func(t *testing.T) {
language, err := gitRepo.GitAttributeFirst("8fee858da5796dfb37704761701bb8e800ad9ef3", "i-am-a-python.p", "linguist-detectable", "gitlab-language", "non-existing")
assert.NoError(t, err)
assert.Equal(t, "", language.String())
})
}
func TestGitAttributeStruct(t *testing.T) {
assert.Equal(t, "", GitAttribute("").String())
assert.Equal(t, "", GitAttribute("unspecified").String())
assert.Equal(t, "python", GitAttribute("python").String())
assert.Equal(t, "text?token=Error", GitAttribute("text?token=Error").String())
assert.Equal(t, "text", GitAttribute("text?token=Error").Prefix())
}
func TestGitAttributeCheckerError(t *testing.T) {
prepareRepo := func(t *testing.T) *Repository {
t.Helper()
path := t.TempDir()
// we can't use unittest.CopyDir because of an import cycle (git.Init in unittest)
require.NoError(t, CopyFS(path, os.DirFS(filepath.Join(testReposDir, "language_stats_repo"))))
gitRepo, err := openRepositoryWithDefaultContext(path)
require.NoError(t, err)
return gitRepo
}
t.Run("RemoveAll/BeforeRun", func(t *testing.T) {
gitRepo := prepareRepo(t)
defer gitRepo.Close()
assert.NoError(t, os.RemoveAll(gitRepo.Path))
ac, err := gitRepo.GitAttributeChecker("", "linguist-language")
require.NoError(t, err)
_, err = ac.CheckPath("i-am-a-python.p")
assert.Error(t, err)
assert.Contains(t, err.Error(), `git check-attr (stderr: ""):`)
})
t.Run("RemoveAll/DuringRun", func(t *testing.T) {
gitRepo := prepareRepo(t)
defer gitRepo.Close()
ac, err := gitRepo.GitAttributeChecker("", "linguist-language")
require.NoError(t, err)
// calling CheckPath before would allow git to cache part of it and succesfully return later
assert.NoError(t, os.RemoveAll(gitRepo.Path))
_, err = ac.CheckPath("i-am-a-python.p")
assert.Error(t, err)
// Depending on the order of execution, the returned error can be:
// - a launch error "fork/exec /usr/bin/git: no such file or directory" (when the removal happens before the Run)
// - a git error (stderr: "fatal: Unable to read current working directory: No such file or directory"): exit status 128 (when the removal happens after the Run)
// (pipe error "write |1: broken pipe" should be replaced by one of the Run errors above)
assert.Contains(t, err.Error(), `git check-attr`)
})
t.Run("Cancelled/BeforeRun", func(t *testing.T) {
gitRepo := prepareRepo(t)
defer gitRepo.Close()
var cancel context.CancelFunc
gitRepo.Ctx, cancel = context.WithCancel(gitRepo.Ctx)
cancel()
ac, err := gitRepo.GitAttributeChecker("8fee858da5796dfb37704761701bb8e800ad9ef3", "linguist-language")
require.NoError(t, err)
_, err = ac.CheckPath("i-am-a-python.p")
assert.ErrorIs(t, err, context.Canceled)
})
t.Run("Cancelled/DuringRun", func(t *testing.T) {
gitRepo := prepareRepo(t)
defer gitRepo.Close()
var cancel context.CancelFunc
gitRepo.Ctx, cancel = context.WithCancel(gitRepo.Ctx)
ac, err := gitRepo.GitAttributeChecker("8fee858da5796dfb37704761701bb8e800ad9ef3", "linguist-language")
require.NoError(t, err)
attr, err := ac.CheckPath("i-am-a-python.p")
assert.NoError(t, err)
assert.Equal(t, "Python", attr["linguist-language"].String())
errCh := make(chan error)
go func() {
cancel()
for err == nil {
_, err = ac.CheckPath("i-am-a-python.p")
runtime.Gosched() // the cancellation must have time to propagate
}
errCh <- err
}()
select {
case <-time.After(time.Second):
t.Error("CheckPath did not complete within 1s")
case err = <-errCh:
assert.ErrorIs(t, err, context.Canceled)
}
})
t.Run("Closed/BeforeRun", func(t *testing.T) {
gitRepo := prepareRepo(t)
defer gitRepo.Close()
ac, err := gitRepo.GitAttributeChecker("8fee858da5796dfb37704761701bb8e800ad9ef3", "linguist-language")
require.NoError(t, err)
assert.NoError(t, ac.Close())
_, err = ac.CheckPath("i-am-a-python.p")
assert.ErrorIs(t, err, fs.ErrClosed)
})
t.Run("Closed/DuringRun", func(t *testing.T) {
gitRepo := prepareRepo(t)
defer gitRepo.Close()
ac, err := gitRepo.GitAttributeChecker("8fee858da5796dfb37704761701bb8e800ad9ef3", "linguist-language")
require.NoError(t, err)
attr, err := ac.CheckPath("i-am-a-python.p")
assert.NoError(t, err)
assert.Equal(t, "Python", attr["linguist-language"].String())
assert.NoError(t, ac.Close())
_, err = ac.CheckPath("i-am-a-python.p")
assert.ErrorIs(t, err, fs.ErrClosed)
})
}
// CopyFS is adapted from https://github.com/golang/go/issues/62484
// which should be available with go1.23
func CopyFS(dir string, fsys fs.FS) error {
return fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, _ error) error {
targ := filepath.Join(dir, filepath.FromSlash(path))
if d.IsDir() {
return os.MkdirAll(targ, 0o777)
}
r, err := fsys.Open(path)
if err != nil {
return err
}
defer r.Close()
info, err := r.Stat()
if err != nil {
return err
}
w, err := os.OpenFile(targ, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o666|info.Mode()&0o777)
if err != nil {
return err
}
if _, err := io.Copy(w, r); err != nil {
w.Close()
return fmt.Errorf("copying %s: %v", path, err)
}
return w.Close()
})
}