Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rules_go] show x archive isn't stable to package interface #4143

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tests/core/go_library/reproducible_x/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
39 changes: 39 additions & 0 deletions tests/core/go_library/reproducible_x/calculator.go
Original file line number Diff line number Diff line change
@@ -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
}
30 changes: 30 additions & 0 deletions tests/core/go_library/reproducible_x/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
37 changes: 37 additions & 0 deletions tests/core/go_library/reproducible_x/show_not_reproducible.sh
Original file line number Diff line number Diff line change
@@ -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