Skip to content

Commit

Permalink
[WIP] testscript experiment
Browse files Browse the repository at this point in the history
Use github.com/rogpeppe/go-internal/testscript instead of ginkgo to
test builder commands?

Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti committed Dec 20, 2024
1 parent 3e24250 commit ebd418d
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 39 deletions.
3 changes: 3 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// SPDX-License-Identifier: Apache-2.0

package cmd
44 changes: 44 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: Apache-2.0

package cmd_test

import (
"os"
"testing"

"github.com/hyperledger-labs/fabric-builder-k8s/cmd"
"github.com/rogpeppe/go-internal/testscript"
)

func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
// "build": cmd.Build,
"detect": cmd.Detect,
// "release": cmd.Release,
// "run": cmd.Run,
}))
}

func TestBuildCommand(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/build",
})
}

func TestDetectCommand(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/detect",
})
}

func TestReleaseCommand(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/release",
})
}

func TestRunCommand(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/run",
})
}
52 changes: 52 additions & 0 deletions cmd/detect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"context"
"errors"
"os"

"github.com/hyperledger-labs/fabric-builder-k8s/internal/builder"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/log"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/util"
)

const (
expectedArgsLength = 3
chaincodeSourceDirectoryArg = 1
chaincodeMetadataDirectoryArg = 2
)

func Detect() int {
debug := util.GetOptionalEnv(util.DebugVariable, "false")
ctx := log.NewCmdContext(context.Background(), debug == "true")
logger := log.New(ctx)

if len(os.Args) != expectedArgsLength {
logger.Println("Expected CHAINCODE_SOURCE_DIR and CHAINCODE_METADATA_DIR arguments")
return 1

Check failure on line 28 in cmd/detect.go

View workflow job for this annotation

GitHub Actions / lint

return with no blank line before (nlreturn)
}

chaincodeSourceDirectory := os.Args[chaincodeSourceDirectoryArg]
chaincodeMetadataDirectory := os.Args[chaincodeMetadataDirectoryArg]

logger.Debugf("Chaincode source directory: %s", chaincodeSourceDirectory)
logger.Debugf("Chaincode metadata directory: %s", chaincodeMetadataDirectory)

detect := &builder.Detect{
ChaincodeSourceDirectory: chaincodeSourceDirectory,
ChaincodeMetadataDirectory: chaincodeMetadataDirectory,
}

if err := detect.Run(ctx); err != nil {
if !errors.Is(err, builder.ErrUnsupportedChaincodeType) {
// don't spam the peer log if it's just chaincode we don't recognise
logger.Printf("Error detecting chaincode: %+v", err)
}

return 1
}

return 0
}
41 changes: 2 additions & 39 deletions cmd/detect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,11 @@
package main

import (
"context"
"errors"
"os"

"github.com/hyperledger-labs/fabric-builder-k8s/internal/builder"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/log"
"github.com/hyperledger-labs/fabric-builder-k8s/internal/util"
)

const (
expectedArgsLength = 3
chaincodeSourceDirectoryArg = 1
chaincodeMetadataDirectoryArg = 2
"github.com/hyperledger-labs/fabric-builder-k8s/cmd"
)

func main() {
debug := util.GetOptionalEnv(util.DebugVariable, "false")
ctx := log.NewCmdContext(context.Background(), debug == "true")
logger := log.New(ctx)

if len(os.Args) != expectedArgsLength {
logger.Println("Expected CHAINCODE_SOURCE_DIR and CHAINCODE_METADATA_DIR arguments")
os.Exit(1)
}

chaincodeSourceDirectory := os.Args[chaincodeSourceDirectoryArg]
chaincodeMetadataDirectory := os.Args[chaincodeMetadataDirectoryArg]

logger.Debugf("Chaincode source directory: %s", chaincodeSourceDirectory)
logger.Debugf("Chaincode metadata directory: %s", chaincodeMetadataDirectory)

detect := &builder.Detect{
ChaincodeSourceDirectory: chaincodeSourceDirectory,
ChaincodeMetadataDirectory: chaincodeMetadataDirectory,
}

if err := detect.Run(ctx); err != nil {
if !errors.Is(err, builder.ErrUnsupportedChaincodeType) {
// don't spam the peer log if it's just chaincode we don't recognise
logger.Printf("Error detecting chaincode: %+v", err)
}

os.Exit(1)
}
os.Exit(cmd.Detect())
}
3 changes: 3 additions & 0 deletions cmd/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// SPDX-License-Identifier: Apache-2.0

package cmd
3 changes: 3 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// SPDX-License-Identifier: Apache-2.0

package cmd
2 changes: 2 additions & 0 deletions cmd/testdata/build/build.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exec bork
stdout 'hello world\n'
2 changes: 2 additions & 0 deletions cmd/testdata/detect/detect.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exec detect
stdout 'hello world\n'
2 changes: 2 additions & 0 deletions cmd/testdata/release/release.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exec release
stdout 'hello world\n'
2 changes: 2 additions & 0 deletions cmd/testdata/run/run.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exec run
stdout 'hello world\n'
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/tools v0.26.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down

0 comments on commit ebd418d

Please sign in to comment.