File: protocol.go

package info (click to toggle)
gitlab-shell 14.35.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,652 kB
  • sloc: ruby: 1,129; makefile: 583; sql: 391; sh: 384
file content (51 lines) | stat: -rw-r--r-- 1,501 bytes parent folder | download | duplicates (4)
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
package gittest

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/v16/internal/git"
	"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config"
	"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
)

// ProtocolDetectingCommandFactory is an intercepting Git command factory that allows the protocol
// to be tested.
type ProtocolDetectingCommandFactory struct {
	git.CommandFactory
	envPath string
}

// NewProtocolDetectingCommandFactory returns a new ProtocolDetectingCommandFactory.
func NewProtocolDetectingCommandFactory(tb testing.TB, ctx context.Context, cfg config.Cfg) ProtocolDetectingCommandFactory {
	envPath := filepath.Join(testhelper.TempDir(tb), "git-env")

	gitCmdFactory := NewInterceptingCommandFactory(tb, ctx, cfg, func(execEnv git.ExecutionEnvironment) string {
		return fmt.Sprintf(
			`#!/usr/bin/env bash
			env | grep ^GIT_PROTOCOL= >>%q
			exec %q "$@"
		`, envPath, execEnv.BinaryPath)
	})

	return ProtocolDetectingCommandFactory{
		CommandFactory: gitCmdFactory,
		envPath:        envPath,
	}
}

// ReadProtocol reads the protocol used by previous Git executions.
func (p *ProtocolDetectingCommandFactory) ReadProtocol(t *testing.T) string {
	data, err := os.ReadFile(p.envPath)
	require.NoError(t, err)
	return string(data)
}

// Reset resets previously recorded protocols.
func (p *ProtocolDetectingCommandFactory) Reset(t *testing.T) {
	require.NoError(t, os.RemoveAll(p.envPath))
}