Skip to content

Commit

Permalink
Merge pull request #286 from Philip-21/cmdtests
Browse files Browse the repository at this point in the history
cmd tests implementations
  • Loading branch information
nguyer authored Feb 15, 2024
2 parents f489c54 + c7061ab commit f36db31
Show file tree
Hide file tree
Showing 142 changed files with 2,301 additions and 2,007 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.18
go-version: "1.21"
- name: Compile FireFly CLI
run: make
10 changes: 5 additions & 5 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ jobs:
fail-fast: false
steps:
- name: Checkout FireFly CLI
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
path: firefly-cli
fetch-depth: 0
- name: Checkout FireFly Core repo
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: hyperledger/firefly
path: firefly

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.18
go-version: "1.21"

- name: Compile FireFly CLI
working-directory: firefly-cli
Expand All @@ -63,7 +63,7 @@ jobs:
run: ./test/e2e/run.sh

- name: Archive container logs
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
if: always()
with:
name: container-logs-${{ matrix.test-suite }}-${{ matrix.blockchain-provider }}-${{ matrix.database-type }}-${{ matrix.token-provider }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.18
go-version: "1.21"
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
with:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright © 2021 Kaleido, Inc.
# Copyright © 2024 Kaleido, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
Expand All @@ -22,3 +22,4 @@ dist/
*.iml
.idea/
docs/command_docs
coverage.txt
66 changes: 66 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
run:
tests: false
skip-dirs:
- "mocks"
- "ffconfig"
- "test/e2e"
linters-settings:
golint: {}
gocritic:
enabled-checks: []
disabled-checks:
- regexpMust
revive:
rules:
- name: unused-parameter
disabled: true
gosec:
excludes:
- G306 # Needed file permission for local development
- G601 # Appears not to handle taking an address of a sub-structure, within a pointer to a structure within a loop. Which is valid and safe.
goheader:
values:
regexp:
COMPANY: .*
template: |-
Copyright © {{ YEAR }} {{ COMPANY }}
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
linters:
disable-all: false
disable:
- structcheck
enable:
- dogsled
- errcheck
- goconst
- gocritic
- gocyclo
- gofmt
- goheader
- goimports
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unused
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright © 2022 Kaleido, Inc.
# Copyright © 2024 Kaleido, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
Expand Down
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright © 2022 Kaleido, Inc.
# Copyright © 2024 Kaleido, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
Expand All @@ -20,7 +20,9 @@ GITREF := $(shell git rev-parse --short HEAD)
DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
LINT := $(GOBIN)/golangci-lint

all: build
all: build lint test tidy
test: deps
$(VGO) test ./internal/... ./pkg/... ./cmd/... -cover -coverprofile=coverage.txt -covermode=atomic -timeout=30s ${TEST_ARGS}
build: ## Builds all go code
cd ff && go build -ldflags="-X 'github.com/hyperledger/firefly-cli/cmd.BuildDate=$(DATE)' -X 'github.com/hyperledger/firefly-cli/cmd.BuildCommit=$(GITREF)'"
install: ## Installs the package
Expand All @@ -31,10 +33,12 @@ lint: ${LINT} ## Checks and reports lint errors

${LINT}:
$(VGO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest


deps:
cd ff && $(VGO) get
help: ## Show this help
@echo 'usage: make [target] ...'
@echo ''
@echo 'targets:'
@egrep '^(.+)\:\ .*##\ (.+)' ${MAKEFILE_LIST} | sed 's/:.*##/#/' | column -t -c 2 -s '#'
@egrep '^(.+)\:\ .*##\ (.+)' ${MAKEFILE_LIST} | sed 's/:.*##/#/' | column -t -c 2 -s '#'
tidy:
$(VGO) mod tidy
2 changes: 1 addition & 1 deletion cmd/accounts.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
2 changes: 1 addition & 1 deletion cmd/accounts_create.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
78 changes: 78 additions & 0 deletions cmd/accounts_create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cmd

import (
"os"
"testing"

"github.com/hyperledger/firefly-cli/internal/utils"
"github.com/stretchr/testify/assert"
)

func TestCreateAccountCmd(t *testing.T) {

testcases := []struct {
Name string
Args []string
ExpectedResponse string
}{
{
Name: "testcase1",
Args: []string{"create", "stack-1"},
ExpectedResponse: "",
},
{
Name: "testcase-2",
Args: []string{"create", "stack-2"},
ExpectedResponse: "",
},
{
Name: "testcase-3",
Args: []string{"create", "stack-3"},
ExpectedResponse: "",
},
{
Name: "testcase-4",
Args: []string{"create", "stack-4"},
ExpectedResponse: "",
},
{
Name: "testcase-5",
Args: []string{"create", "stack-5"},
ExpectedResponse: "",
},
{
Name: "testcase-6",
Args: []string{"create", "stack-6"},
ExpectedResponse: "",
},
}
for _, tc := range testcases {
t.Run(tc.Name, func(t *testing.T) {

cmd := accountsCreateCmd
cmd.SetArgs(tc.Args)

// Capture the output
originalOutput, outputBuffer := utils.CaptureOutput()
defer func() {
// Restore the original output after capturing
os.Stdout = originalOutput
}()
cmd.SetOut(outputBuffer)

// Execute the command
err := cmd.Execute()
if err != nil {
t.Fatalf("Command execution failed: %v", err)
}

// Get the actual response
actualResponse := outputBuffer.String()

// Compare actual and expected responses
assert.Equal(t, tc.ExpectedResponse, actualResponse, "Responses do not match")

assert.NotNil(t, actualResponse)
})
}
}
2 changes: 1 addition & 1 deletion cmd/accounts_list.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
38 changes: 38 additions & 0 deletions cmd/accounts_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"testing"

"github.com/hyperledger/firefly-cli/internal/utils"
"github.com/stretchr/testify/assert"
)

func TestAccountListCmd(t *testing.T) {
testNames := []string{"stack-1", "stack-2", "stack-3","stack-4", "stack-5"}
for _, stackNames := range testNames {
createCmd := accountsCreateCmd
createCmd.SetArgs([]string{"ff", "create", stackNames})
err := createCmd.Execute()
if err != nil {
t.Fatalf("Failed to create account for testing: %v", err)
}
Args := []string{ "ls"}
t.Run("Test-Account-List", func(t *testing.T) {
cmd := accountsListCmd
cmd.SetArgs(Args)

_, outputBuffer := utils.CaptureOutput()
cmd.SetOut(outputBuffer)

err := cmd.Execute()
if err != nil {
t.Fatalf("Command execution failed: %v", err)
}
actualResponse := outputBuffer.String()

assert.NotNil(t, actualResponse)

})
}

}
2 changes: 1 addition & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
2 changes: 1 addition & 1 deletion cmd/deploy_ethereum.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
40 changes: 40 additions & 0 deletions cmd/deploy_ethereum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

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

"github.com/hyperledger/firefly-cli/internal/utils"
"github.com/stretchr/testify/assert"
)

func TestDeployEthereumCmd(t *testing.T) {
var ctx context.Context
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()

createcmd := accountsCreateCmd
createcmd.SetArgs([]string{"create", "stack-2"})
err := createcmd.ExecuteContext(ctx)
if err != nil {
t.Fatalf("unable to create stack : %v", err)
}
currDir := t.TempDir()
contractFile := filepath.Join(currDir + "eth_deploy.json")
Args := []string{"deploy", "ethereum", "stack-2", contractFile, "param1", "param2"}
ethDeployCmd := deployEthereumCmd
ethDeployCmd.SetArgs(Args)
ethDeployCmd.ExecuteContext(ctx)

Outputwriter, outputBuffer := utils.CaptureOutput()
defer func() {
os.Stdout = Outputwriter
}()
ethDeployCmd.SetOutput(outputBuffer)

actualResponse := outputBuffer.String()
assert.NotNil(t, actualResponse)
}
2 changes: 1 addition & 1 deletion cmd/deploy_fabric.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
Loading

0 comments on commit f36db31

Please sign in to comment.