diff --git a/.gitignore b/.gitignore index 9c89a30..9a3e35f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ bats/ bats-mock/ *.tgz **/.DS_Store +TEST-report.xml +tst-output.txt diff --git a/README.md b/README.md index 40bfd80..4da11d8 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ github.com/hyperledger/fabric/core/chaincode/lib/cid@v1.2.1 ## 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. diff --git a/src/go-chaincode/vendor-dependencies.sh b/src/go-chaincode/vendor-dependencies.sh index cd0a424..9faac76 100755 --- a/src/go-chaincode/vendor-dependencies.sh +++ b/src/go-chaincode/vendor-dependencies.sh @@ -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 -r package; do + packages+=($package) + done < .govendor_packages + local index=0 shopt -s extglob while (( ${#packages[@]} > index )); do @@ -80,4 +82,4 @@ function _fetch_dependencies_cc { popd echo "Finished looking up dependencies for chaincode component." -} \ No newline at end of file +} diff --git a/test/go-chaincode/vendor-dependencies.bats b/test/go-chaincode/vendor-dependencies.bats index bc7ff04..7ec4f23 100644 --- a/test/go-chaincode/vendor-dependencies.bats +++ b/test/go-chaincode/vendor-dependencies.bats @@ -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" { @@ -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": [ @@ -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'" ] @@ -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": [ @@ -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" @@ -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/cid@v1.2.1 + github.com/hyperledger/fabric/core/chaincode/lib/cid@v1.2.1 + github.com/hyperledger/fabric/core/chaincode/lib/conga@v1.5.8 @@ -130,8 +137,9 @@ EOF stub govendor \ "init : true" \ "fetch *github.com/hyperledger/fabric/core/chaincode/lib/cid@v1.2.1* : true" \ - "init : true" \ - "fetch *github.com/hyperledger/fabric/core/chaincode/lib/cid@v1.2.1* : true" + "init : true" \ + "fetch *github.com/hyperledger/fabric/core/chaincode/lib/cid@v1.2.1* : true" \ + "fetch *github.com/hyperledger/fabric/core/chaincode/lib/conga@v1.5.8* : true" stub cp \ "-r vendor : true" \ @@ -139,17 +147,13 @@ EOF 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'" ] @@ -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/cid@v1.2.1" ] - [ "${lines[14]}" = "Finished looking up dependencies for chaincode component." ] -} - -teardown() { - cleanup_stubs + [ "${lines[12]}" = "Fetching github.com/hyperledger/fabric/core/chaincode/lib/conga@v1.5.8" ] + [ "${lines[15]}" = "Finished looking up dependencies for chaincode component." ] }