Skip to content

Commit

Permalink
Remove readarray requirement
Browse files Browse the repository at this point in the history
Includes minor changes to testcases to keep temporary test files inside the testcases' temporary directory

Related to IBM-Blockchain-Starter-Kit#87

Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti committed Mar 8, 2019
1 parent 9730637 commit f705a06
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ bats/
bats-mock/
*.tgz
**/.DS_Store
TEST-report.xml
tst-output.txt
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ github.com/hyperledger/fabric/core/chaincode/lib/[email protected]

## Unit tests

Make sure you have installed [jq](https://stedolan.github.io/jq/download/) before attempting to run the test cases on your local system. You should also have bash 4 or a later version installed on your system. If running tests on macOS, you can use Homebrew to upgrade bash: `brew update && brew install bash`.
Make sure you have installed [jq](https://stedolan.github.io/jq/download/) before attempting to run the test cases on your local system.

This project is tested using Bats when making pull requests.

Expand Down
10 changes: 6 additions & 4 deletions src/go-chaincode/vendor-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ function _fetch_dependencies_cc {
# Initialize govendor
govendor init
# Get list of packages to vendor in
declare -a packages
readarray packages < .govendor_packages
local index=0
declare -a packages
while read package; do
packages+=($package)
done < .govendor_packages
local index=0

shopt -s extglob
while (( ${#packages[@]} > index )); do
Expand Down Expand Up @@ -80,4 +82,4 @@ function _fetch_dependencies_cc {
popd

echo "Finished looking up dependencies for chaincode component."
}
}
59 changes: 30 additions & 29 deletions test/go-chaincode/vendor-dependencies.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ setup() {
testcase_dirname="$(mktemp -d)"

setup_script_dir "${src_dir}" "${testcase_dirname}"

testcase_gopath=${testcase_dirname}/go
echo "export GOPATH=${testcase_gopath}" >> "${SCRIPT_DIR}/common/env.sh"
}

teardown() {
cleanup_stubs
}

@test "vendor-dependencies.sh: should exist and be executable" {
Expand All @@ -16,7 +23,8 @@ setup() {

@test "vendor-dependencies.sh: fetch_dependencies should run without errors when .govendor_packages file does not exist" {

cat << EOF > sample-config.json
mkdir -p "${testcase_gopath}/src"
cat << EOF > "${testcase_gopath}/src/sample-config.json"
{
"org1": {
"chaincode": [
Expand Down Expand Up @@ -45,21 +53,18 @@ setup() {
}
EOF

mkdir -p "${PWD}/src/chaincode/contract1"
mkdir -p "${testcase_gopath}/src/chaincode/contract1"

stub go \
"get -u github.com/kardianos/govendor : true"

source "${SCRIPT_DIR}/go-chaincode/vendor-dependencies.sh"

run fetch_dependencies "sample-config.json"

# Clean up before assertions
rm sample-config.json
rm -rf "${PWD}/src/chaincode"
unstub go
run fetch_dependencies "${testcase_gopath}/src/sample-config.json"

# Assertions
echo $output
unstub go
[ $status -eq 0 ]
[ "${lines[0]}" = "Processing org 'org1'..." ]
[ "${lines[1]}" = "About to fetch dependencies for 'chaincode/contract1'" ]
Expand All @@ -72,7 +77,8 @@ EOF

@test "vendor-dependencies.sh: fetch_dependencies should run without errors when .govendor_packages file has new lines and white spaces; also testing with multiple .govendor_packages file (one per chaincode component)" {

cat << EOF > sample-config.json
mkdir -p "${testcase_gopath}/src"
cat << EOF > "${testcase_gopath}/src/sample-config.json"
{
"org1": {
"chaincode": [
Expand All @@ -97,10 +103,10 @@ EOF
}
EOF

mkdir -p "${PWD}/src/chaincode/contract1"
mkdir -p "${PWD}/src/chaincode/contract2"
mkdir -p "${testcase_gopath}/src/chaincode/contract1"
mkdir -p "${testcase_gopath}/src/chaincode/contract2"

cat << EOF > "${PWD}/src/chaincode/contract1/.govendor_packages"
cat << EOF > "${testcase_gopath}/src/chaincode/contract1/.govendor_packages"
Expand All @@ -112,11 +118,12 @@ EOF
EOF

cat << EOF > "${PWD}/src/chaincode/contract2/.govendor_packages"
cat << EOF > "${testcase_gopath}/src/chaincode/contract2/.govendor_packages"
github.com/hyperledger/fabric/core/chaincode/lib/[email protected]
github.com/hyperledger/fabric/core/chaincode/lib/[email protected]
github.com/hyperledger/fabric/core/chaincode/lib/[email protected]
Expand All @@ -130,26 +137,23 @@ EOF
stub govendor \
"init : true" \
"fetch *github.com/hyperledger/fabric/core/chaincode/lib/[email protected]* : true" \
"init : true" \
"fetch *github.com/hyperledger/fabric/core/chaincode/lib/[email protected]* : true"
"init : true" \
"fetch *github.com/hyperledger/fabric/core/chaincode/lib/[email protected]* : true" \
"fetch *github.com/hyperledger/fabric/core/chaincode/lib/[email protected]* : true"

stub cp \
"-r vendor : true" \
"-r vendor : true"

source "${SCRIPT_DIR}/go-chaincode/vendor-dependencies.sh"

run fetch_dependencies "sample-config.json"

# Clean up before assertions
rm sample-config.json
rm -rf "${PWD}/src/chaincode"
unstub cp
unstub govendor
unstub go
run fetch_dependencies "${testcase_gopath}/src/sample-config.json"

# Assertions
echo $output
unstub cp
unstub govendor
unstub go
[ $status -eq 0 ]
[ "${lines[0]}" = "Processing org 'org1'..." ]
[ "${lines[1]}" = "About to fetch dependencies for 'chaincode/contract1'" ]
Expand All @@ -159,9 +163,6 @@ EOF
[ "${lines[8]}" = "About to fetch dependencies for 'chaincode/contract2'" ]
[ "${lines[10]}" = "Found .govendor_packages file." ]
[ "${lines[11]}" = "Fetching github.com/hyperledger/fabric/core/chaincode/lib/[email protected]" ]
[ "${lines[14]}" = "Finished looking up dependencies for chaincode component." ]
}

teardown() {
cleanup_stubs
[ "${lines[12]}" = "Fetching github.com/hyperledger/fabric/core/chaincode/lib/[email protected]" ]
[ "${lines[15]}" = "Finished looking up dependencies for chaincode component." ]
}

0 comments on commit f705a06

Please sign in to comment.