From 4a3ece0c6c22e6a3f376c26d38eb4dbad8ada420 Mon Sep 17 00:00:00 2001 From: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> Date: Fri, 9 Aug 2024 10:53:29 +0000 Subject: [PATCH] [bugfix] ensure testrig package only compiled-in when debug enabled (#3185) * ensure testrig package only compiled-in (including init) when debug enabled * add code comment to testrig init to indicate WebAssembly compilation --- cmd/gotosocial/action/testrig/no_testrig.go | 26 ++++++++++++++++++ cmd/gotosocial/action/testrig/testrig.go | 5 +++- cmd/gotosocial/main.go | 10 +++---- cmd/gotosocial/testrig.go | 29 ++++++++++++--------- testrig/config.go | 3 +++ 5 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 cmd/gotosocial/action/testrig/no_testrig.go diff --git a/cmd/gotosocial/action/testrig/no_testrig.go b/cmd/gotosocial/action/testrig/no_testrig.go new file mode 100644 index 000000000..7205ecf6b --- /dev/null +++ b/cmd/gotosocial/action/testrig/no_testrig.go @@ -0,0 +1,26 @@ +// GoToSocial +// Copyright (C) GoToSocial Authors admin@gotosocial.org +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +//go:build !debug && !debugenv + +package testrig + +import "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action" + +// Start creates and starts a gotosocial testrig server. +// This is only enabled in debug builds, else is nil. +var Start action.GTSAction diff --git a/cmd/gotosocial/action/testrig/testrig.go b/cmd/gotosocial/action/testrig/testrig.go index 95982fc72..19588c70a 100644 --- a/cmd/gotosocial/action/testrig/testrig.go +++ b/cmd/gotosocial/action/testrig/testrig.go @@ -15,6 +15,8 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +//go:build debug || debugenv + package testrig import ( @@ -52,7 +54,8 @@ "github.com/superseriousbusiness/gotosocial/testrig" ) -// Start creates and starts a gotosocial testrig server +// Start creates and starts a gotosocial testrig server. +// This is only enabled in debug builds, else is nil. var Start action.GTSAction = func(ctx context.Context) error { testrig.InitTestConfig() testrig.InitTestLog() diff --git a/cmd/gotosocial/main.go b/cmd/gotosocial/main.go index 5c610f1a3..7d45c126d 100644 --- a/cmd/gotosocial/main.go +++ b/cmd/gotosocial/main.go @@ -23,7 +23,6 @@ godebug "runtime/debug" "strings" - "codeberg.org/gruf/go-debug" "github.com/spf13/cobra" _ "github.com/superseriousbusiness/gotosocial/docs" @@ -63,11 +62,12 @@ func main() { rootCmd.AddCommand(serverCommands()) rootCmd.AddCommand(debugCommands()) rootCmd.AddCommand(adminCommands()) - if debug.DEBUG { - // only add testrig if debug enabled. - rootCmd.AddCommand(testrigCommands()) + + // Testrigcmd will only be set when debug is enabled. + if testrigCmd := testrigCommands(); testrigCmd != nil { + rootCmd.AddCommand(testrigCmd) } else if len(os.Args) > 1 && os.Args[1] == "testrig" { - log.Fatalln("gotosocial must be built and run with the DEBUG enviroment variable set to enable and access testrig") + log.Fatal("gotosocial must be built and run with the DEBUG enviroment variable set to enable and access testrig") } // run diff --git a/cmd/gotosocial/testrig.go b/cmd/gotosocial/testrig.go index 20cca9f1c..124fc1105 100644 --- a/cmd/gotosocial/testrig.go +++ b/cmd/gotosocial/testrig.go @@ -23,19 +23,22 @@ ) func testrigCommands() *cobra.Command { - testrigCmd := &cobra.Command{ - Use: "testrig", - Short: "gotosocial testrig-related tasks", - } + if testrig.Start != nil { + testrigCmd := &cobra.Command{ + Use: "testrig", + Short: "gotosocial testrig-related tasks", + } - testrigStartCmd := &cobra.Command{ - Use: "start", - Short: "start the gotosocial testrig server", - RunE: func(cmd *cobra.Command, args []string) error { - return run(cmd.Context(), testrig.Start) - }, - } + testrigStartCmd := &cobra.Command{ + Use: "start", + Short: "start the gotosocial testrig server", + RunE: func(cmd *cobra.Command, args []string) error { + return run(cmd.Context(), testrig.Start) + }, + } - testrigCmd.AddCommand(testrigStartCmd) - return testrigCmd + testrigCmd.AddCommand(testrigStartCmd) + return testrigCmd + } + return nil } diff --git a/testrig/config.go b/testrig/config.go index ed98798d6..6a4254d61 100644 --- a/testrig/config.go +++ b/testrig/config.go @@ -19,6 +19,7 @@ import ( "context" + "fmt" "os" "strconv" "time" @@ -34,11 +35,13 @@ func init() { ctx := context.Background() // Ensure global ffmpeg WASM pool initialized. + fmt.Println("testrig: precompiling ffmpeg WASM") if err := ffmpeg.InitFfmpeg(ctx, 1); err != nil { panic(err) } // Ensure global ffmpeg WASM pool initialized. + fmt.Println("testrig: precompiling ffprobe WASM") if err := ffmpeg.InitFfprobe(ctx, 1); err != nil { panic(err) }