From 67289f51f30eec440fbb41ff43c7a5fa47436565 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 24 May 2023 16:32:17 +0000 Subject: [PATCH] test: disable hooks on testscript --- server/backend/sqlite/sqlite.go | 3 ++ server/hooks/hooks.go | 8 ++--- testscript/script_test.go | 57 +++++++++++++++++++++++++-------- 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/server/backend/sqlite/sqlite.go b/server/backend/sqlite/sqlite.go index 91527e3fcc0a82b8b35e8f1cba9eab55477cd8bb..d38ae26b8d0e801f2771a5346329dd10658bdbc9 100644 --- a/server/backend/sqlite/sqlite.go +++ b/server/backend/sqlite/sqlite.go @@ -603,6 +603,9 @@ func (d *SqliteBackend) RemoveCollaborator(repo string, username string) error { } func (d *SqliteBackend) initRepo(repo string) error { + if os.Getenv("SOFT_SERVE_TEST_NO_HOOKS") == "1" { + return nil + } return hooks.GenerateHooks(d.ctx, d.cfg, repo) } diff --git a/server/hooks/hooks.go b/server/hooks/hooks.go index f7606bdd599d66ffcc0bb4cac24684fb1b1a3ca8..08968ed1056f46c7313279025a3c134633456056 100644 --- a/server/hooks/hooks.go +++ b/server/hooks/hooks.go @@ -136,10 +136,9 @@ done ` ) -var ( - // hooksTmpl is the soft-serve hook that will be run by the git hooks - // inside the hooks directory. - hooksTmpl = template.Must(template.New("hooks").Parse(`#!/usr/bin/env bash +// hooksTmpl is the soft-serve hook that will be run by the git hooks +// inside the hooks directory. +var hooksTmpl = template.Must(template.New("hooks").Parse(`#!/usr/bin/env bash # AUTO GENERATED BY SOFT SERVE, DO NOT MODIFY if [ -z "$SOFT_SERVE_REPO_NAME" ]; then echo "Warning: SOFT_SERVE_REPO_NAME not defined. Skipping hooks." @@ -149,4 +148,3 @@ fi {{ $env }} \{{ end }} {{ .Executable }} hook --config "{{ .Config }}" {{ .Hook }} {{ .Args }} `)) -) diff --git a/testscript/script_test.go b/testscript/script_test.go index 2e3a20375c5d262aefe2fd75fce10f73372e8517..98b7054f15aa81fc7f93b9c452899f236d4ad169 100644 --- a/testscript/script_test.go +++ b/testscript/script_test.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "sync" "testing" "time" @@ -22,6 +23,8 @@ func TestScript(t *testing.T) { flag.Parse() var lock sync.Mutex + t.Setenv("SOFT_SERVE_TEST_NO_HOOKS", "1") + // we'll use this key to talk with soft serve, and since testscript changes // the cwd, we need to get its full path here key, err := filepath.Abs("./testdata/admin1") @@ -42,24 +45,52 @@ func TestScript(t *testing.T) { } } + sshArgs := []string{ + "-F", "/dev/null", + "-o", "StrictHostKeyChecking=no", + "-o", "UserKnownHostsFile=/dev/null", + "-o", "IdentityAgent=none", + "-o", "IdentitiesOnly=yes", + "-i", key, + } + + check := func(ts *testscript.TestScript, err error, neg bool) { + if neg && err == nil { + ts.Fatalf("expected error, got nil") + } + if !neg { + ts.Check(err) + } + } + testscript.Run(t, testscript.Params{ Dir: "testdata/script", UpdateScripts: *update, Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){ - "soft": func(ts *testscript.TestScript, _ bool, args []string) { + "soft": func(ts *testscript.TestScript, neg bool, args []string) { // TODO: maybe use plain ssh client here? - args = append([]string{ - "-F", "/dev/null", - "-o", "StrictHostKeyChecking=no", - "-o", "UserKnownHostsFile=/dev/null", - "-o", "IdentityAgent=none", - "-o", "IdentitiesOnly=yes", - "-i", key, - "-p", ts.Getenv("SSH_PORT"), - "localhost", - "--", - }, args...) - ts.Check(ts.Exec("ssh", args...)) + args = append( + sshArgs, + append([]string{ + "-p", ts.Getenv("SSH_PORT"), + "localhost", + "--", + }, args...)..., + ) + check(ts, ts.Exec("ssh", args...), neg) + }, + "git": func(ts *testscript.TestScript, _ bool, args []string) { + ts.Setenv( + "GIT_SSH_COMMAND", + strings.Join(append([]string{"ssh"}, sshArgs...), " "), + ) + ts.Check(ts.Exec("git", args...)) + }, + "mkreadme": func(ts *testscript.TestScript, _ bool, args []string) { + if len(args) != 1 { + ts.Fatalf("must have exactly 1 arg, the filename, got %d", len(args)) + } + ts.Check(os.WriteFile(ts.MkAbs(args[0]), []byte("# example\ntest project"), 0o644)) }, }, Setup: func(e *testscript.Env) error {