From 45e0a046400ea3b411822f009c6e59ce1d470feb Mon Sep 17 00:00:00 2001 From: joeljeske Date: Tue, 15 Oct 2024 14:35:21 +0000 Subject: [PATCH] [rules_go] show x archive isn't stable to package interface --- .../go_library/reproducible_x/BUILD.bazel | 22 +++++++++++ .../go_library/reproducible_x/calculator.go | 39 +++++++++++++++++++ tests/core/go_library/reproducible_x/main.go | 30 ++++++++++++++ .../reproducible_x/show_not_reproducible.sh | 37 ++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 tests/core/go_library/reproducible_x/BUILD.bazel create mode 100644 tests/core/go_library/reproducible_x/calculator.go create mode 100644 tests/core/go_library/reproducible_x/main.go create mode 100755 tests/core/go_library/reproducible_x/show_not_reproducible.sh diff --git a/tests/core/go_library/reproducible_x/BUILD.bazel b/tests/core/go_library/reproducible_x/BUILD.bazel new file mode 100644 index 0000000000..2c25032dcf --- /dev/null +++ b/tests/core/go_library/reproducible_x/BUILD.bazel @@ -0,0 +1,22 @@ +load("//go:def.bzl", "go_binary", "go_library") + +go_library( + name = "calculator", + srcs = [ + "calculator.go", + ], + importpath = "calculator", +) + +go_binary( + name = "main", + srcs = [ + "main.go", + ], + deps = [":calculator"], +) + +sh_binary( + name = "show_not_reproducible", + srcs = ["show_not_reproducible.sh"], +) diff --git a/tests/core/go_library/reproducible_x/calculator.go b/tests/core/go_library/reproducible_x/calculator.go new file mode 100644 index 0000000000..8a850bc313 --- /dev/null +++ b/tests/core/go_library/reproducible_x/calculator.go @@ -0,0 +1,39 @@ +package calculator + +import ( + "errors" +) + +// Calculator interface defines the basic operations +type Calculator interface { + Add(a, b float64) float64 + Subtract(a, b float64) float64 + Multiply(a, b float64) float64 + Divide(a, b float64) (float64, error) +} + +// SimpleCalculator struct that implements the Calculator interface +type SimpleCalculator struct{} + +// Add method +func (c SimpleCalculator) Add(a, b float64) float64 { + return a + b +} + +// Subtract method +func (c SimpleCalculator) Subtract(a, b float64) float64 { + return a - b +} + +// Multiply method +func (c SimpleCalculator) Multiply(a, b float64) float64 { + return a * b +} + +// Divide method (with error handling for division by zero) +func (c SimpleCalculator) Divide(a, b float64) (float64, error) { + if b == 0 { + return 0, errors.New("division by zero is not allowed") + } + return a / b, nil +} diff --git a/tests/core/go_library/reproducible_x/main.go b/tests/core/go_library/reproducible_x/main.go new file mode 100644 index 0000000000..53703c1519 --- /dev/null +++ b/tests/core/go_library/reproducible_x/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "calculator" + "fmt" +) + +func main() { + // Create an instance of SimpleCalculator + calculator := calculator.SimpleCalculator{} + + // Perform some operations + fmt.Println("Add: 5 + 3 =", calculator.Add(5, 3)) + fmt.Println("Subtract: 5 - 3 =", calculator.Subtract(5, 3)) + fmt.Println("Multiply: 5 * 3 =", calculator.Multiply(5, 3)) + + result, err := calculator.Divide(5, 0) + if err != nil { + fmt.Println("Error:", err) + } else { + fmt.Println("Divide: 5 / 3 =", result) + } + + result, err = calculator.Divide(5, 3) + if err != nil { + fmt.Println("Error:", err) + } else { + fmt.Println("Divide: 5 / 3 =", result) + } +} diff --git a/tests/core/go_library/reproducible_x/show_not_reproducible.sh b/tests/core/go_library/reproducible_x/show_not_reproducible.sh new file mode 100755 index 0000000000..f5887abb4f --- /dev/null +++ b/tests/core/go_library/reproducible_x/show_not_reproducible.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd "${BUILD_WORKSPACE_DIRECTORY}" +BAZEL_BIN="$(bazel info bazel-bin)" + +tmp_output="$(mktemp -d)" + +function build_and_show_hash() { + echo "Building..." + bazel build //tests/core/go_library/reproducible_x:calculator + printf "\n\n\n" + echo "Taking hash of .x file" + cp "$BAZEL_BIN/tests/core/go_library/reproducible_x/calculator.x" "$tmp_output/calculator_$1.x" + shasum -a 256 "$BAZEL_BIN/tests/core/go_library/reproducible_x/calculator.x" > "$tmp_output/calculator_$1.x.sha256" + printf "\n\n\n" +} + +build_and_show_hash "original" + +echo "Changing the source code" +sed -i 's/division by zero is not allowed/division by zero is not allowed - modified/g' tests/core/go_library/reproducible_x/calculator.go + +build_and_show_hash "modified" + +echo "Comparing the hashes..." +if diff "$tmp_output/calculator_original.x.sha256" "$tmp_output/calculator_modified.x.sha256"; then + echo "Hashes are the same. Output is reproducible" +else + echo + echo + echo "Hashes are different" + echo "Export archives are saved in $tmp_output" + echo + ls -l "$tmp_output" + exit 1 +fi