-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rewrite generic build rules in Go (#1051)
This diff rewrites the generic build rules in Go. See ooni/probe#2401.
- Loading branch information
1 parent
2acefa3
commit 35faf23
Showing
7 changed files
with
168 additions
and
46 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
// | ||
// Generic builder for the current GOOS/GOARCH | ||
// | ||
|
||
import ( | ||
"runtime" | ||
|
||
"github.com/apex/log" | ||
"github.com/ooni/probe-cli/v3/internal/cmd/buildtool/internal/buildtoolmodel" | ||
"github.com/ooni/probe-cli/v3/internal/runtimex" | ||
"github.com/ooni/probe-cli/v3/internal/shellx" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// genericSubcommand returns the generic sucommand. | ||
func genericSubcommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "generic", | ||
Short: "Generic Go builder for the current GOOS and GOARCH", | ||
} | ||
cmd.AddCommand(&cobra.Command{ | ||
Use: "miniooni", | ||
Short: "Builds miniooni for the current GOOS and GOARCH", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
genericBuildPackage(&buildDeps{}, productMiniooni) | ||
}, | ||
}) | ||
cmd.AddCommand(&cobra.Command{ | ||
Use: "ooniprobe", | ||
Short: "Builds ooniprobe for the current GOOS and GOARCH", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
genericBuildPackage(&buildDeps{}, productOoniprobe) | ||
}, | ||
}) | ||
return cmd | ||
} | ||
|
||
// genericBuildPackage is the generic function for building a package. | ||
func genericBuildPackage(deps buildtoolmodel.Dependencies, product *product) { | ||
deps.PsiphonMaybeCopyConfigFiles() | ||
deps.GolangCheck() | ||
|
||
log.Infof("building %s for %s/%s", product.Pkg, runtime.GOOS, runtime.GOARCH) | ||
|
||
argv := runtimex.Try1(shellx.NewArgv("go", "build")) | ||
if deps.PsiphonFilesExist() { | ||
argv.Append("-tags", "ooni_psiphon_config") | ||
} | ||
argv.Append("-ldflags", "-s -w") | ||
argv.Append(product.Pkg) | ||
|
||
config := &shellx.Config{ | ||
Logger: log.Log, | ||
Flags: shellx.FlagShowStdoutStderr, | ||
} | ||
|
||
runtimex.Try0(shellx.RunEx(config, argv, &shellx.Envp{})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/ooni/probe-cli/v3/internal/cmd/buildtool/internal/buildtooltest" | ||
"github.com/ooni/probe-cli/v3/internal/shellx/shellxtesting" | ||
) | ||
|
||
func TestGenericBuildPackage(t *testing.T) { | ||
|
||
// testspec specifies a test case for this test | ||
type testspec struct { | ||
// name is the name of the test case | ||
name string | ||
|
||
// product is the product to build | ||
product *product | ||
|
||
// hasPsiphon indicates whether we should build with psiphon config | ||
hasPsiphon bool | ||
|
||
// expectations contains the commands we expect to see | ||
expect []buildtooltest.ExecExpectations | ||
} | ||
|
||
var testcases = []testspec{{ | ||
name: "miniooni build with psiphon", | ||
product: productMiniooni, | ||
hasPsiphon: true, | ||
expect: []buildtooltest.ExecExpectations{{ | ||
Env: []string{}, | ||
Argv: []string{ | ||
"go", "build", "-tags", "ooni_psiphon_config", | ||
"-ldflags", "-s -w", "./internal/cmd/miniooni", | ||
}, | ||
}}, | ||
}, { | ||
name: "miniooni build without psiphon", | ||
product: productMiniooni, | ||
hasPsiphon: false, | ||
expect: []buildtooltest.ExecExpectations{{ | ||
Env: []string{}, | ||
Argv: []string{ | ||
"go", "build", "-ldflags", "-s -w", "./internal/cmd/miniooni", | ||
}, | ||
}}, | ||
}, { | ||
name: "ooniprobe build with psiphon", | ||
product: productOoniprobe, | ||
hasPsiphon: true, | ||
expect: []buildtooltest.ExecExpectations{{ | ||
Env: []string{}, | ||
Argv: []string{ | ||
"go", "build", "-tags", "ooni_psiphon_config", | ||
"-ldflags", "-s -w", "./cmd/ooniprobe", | ||
}, | ||
}}, | ||
}, { | ||
name: "ooniprobe build without psiphon", | ||
product: productOoniprobe, | ||
hasPsiphon: false, | ||
expect: []buildtooltest.ExecExpectations{{ | ||
Env: []string{}, | ||
Argv: []string{ | ||
"go", "build", "-ldflags", "-s -w", "./cmd/ooniprobe", | ||
}, | ||
}}, | ||
}} | ||
|
||
for _, testcase := range testcases { | ||
t.Run(testcase.name, func(t *testing.T) { | ||
|
||
cc := &buildtooltest.SimpleCommandCollector{} | ||
|
||
deps := &buildtooltest.DependenciesCallCounter{ | ||
HasPsiphon: testcase.hasPsiphon, | ||
} | ||
|
||
shellxtesting.WithCustomLibrary(cc, func() { | ||
genericBuildPackage(deps, testcase.product) | ||
}) | ||
|
||
expectCalls := map[string]int{ | ||
buildtooltest.TagGolangCheck: 1, | ||
buildtooltest.TagPsiphonMaybeCopyConfigFiles: 1, | ||
buildtooltest.TagPsiphonFilesExist: 1, | ||
} | ||
|
||
if diff := cmp.Diff(expectCalls, deps.Counter); diff != "" { | ||
t.Fatal(diff) | ||
} | ||
|
||
if err := buildtooltest.CheckManyCommands(cc.Commands, testcase.expect); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters