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

Move BoxOutput util function to the only place it is called. #16513

Merged
merged 3 commits into from
Feb 21, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changeset/itchy-cameras-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": minor
---

#updated Move BoxOutput util function to the only place it is called.
6 changes: 2 additions & 4 deletions core/gethwrappers/go_generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
cutils "github.com/smartcontractkit/chainlink-common/pkg/utils"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"

"github.com/smartcontractkit/chainlink/v2/core/utils"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -39,7 +37,7 @@ func TestCheckContractHashesFromLastGoGenerate(t *testing.T) {
wd = "<directory containing this test>"
}
require.Equal(t, versions.GethVersion, GethVersion,
color.HiRedString(utils.BoxOutput("please re-run `go generate %s` and commit the"+
color.HiRedString(BoxOutput("please re-run `go generate %s` and commit the"+
"changes", wd)))

for _, contractVersionInfo := range versions.ContractVersions {
Expand Down Expand Up @@ -96,7 +94,7 @@ func compareCurrentCompilerArtifactAgainstRecordsAndSoliditySources(
require.NoError(t, err)
recompileCommand := fmt.Sprintf("(cd %s/contracts; make wrappers-all)", rootDir)
assert.Equal(t, versionInfo.Hash, hash,
utils.BoxOutput(`compiled %s and/or %s has changed; please rerun
BoxOutput(`compiled %s and/or %s has changed; please rerun
%s,
and commit the changes`, versionInfo.AbiPath, versionInfo.BinaryPath, recompileCommand))
}
Expand Down
29 changes: 29 additions & 0 deletions core/gethwrappers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
)

// VersionHash is the hash used to detect changes in the underlying contract
Expand Down Expand Up @@ -61,3 +62,31 @@ func TempDir(dirPrefix string) (string, func()) {
}
}
}

// BoxOutput formats its arguments as fmt.Printf, and encloses them in a box of
// arrows pointing at their content, in order to better highlight it. See
// ExampleBoxOutput
func BoxOutput(errorMsgTemplate string, errorMsgValues ...interface{}) string {
errorMsgTemplate = fmt.Sprintf(errorMsgTemplate, errorMsgValues...)
lines := strings.Split(errorMsgTemplate, "\n")
maxlen := 0
for _, line := range lines {
if len(line) > maxlen {
maxlen = len(line)
}
}
internalLength := maxlen + 4
output := "↘" + strings.Repeat("↓", internalLength) + "↙\n" // top line
output += "→ " + strings.Repeat(" ", maxlen) + " ←\n"
readme := strings.Repeat("README ", maxlen/7)
output += "→ " + readme + strings.Repeat(" ", maxlen-len(readme)) + " ←\n"
output += "→ " + strings.Repeat(" ", maxlen) + " ←\n"
for _, line := range lines {
output += "→ " + line + strings.Repeat(" ", maxlen-len(line)) + " ←\n"
}
output += "→ " + strings.Repeat(" ", maxlen) + " ←\n"
output += "→ " + readme + strings.Repeat(" ", maxlen-len(readme)) + " ←\n"
output += "→ " + strings.Repeat(" ", maxlen) + " ←\n"
return "\n" + output + "↗" + strings.Repeat("↑", internalLength) + "↖" + // bottom line
"\n\n"
}
27 changes: 27 additions & 0 deletions core/gethwrappers/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gethwrappers_test

import (
"testing"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers"

"github.com/stretchr/testify/assert"
)

func TestBoxOutput(t *testing.T) {
t.Parallel()

output := gethwrappers.BoxOutput("some error %d %s", 123, "foo")
const expected = "\n" +
"↘↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↙\n" +
"→ ←\n" +
"→ README README ←\n" +
"→ ←\n" +
"→ some error 123 foo ←\n" +
"→ ←\n" +
"→ README README ←\n" +
"→ ←\n" +
"↗↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↖\n" +
"\n"
assert.Equal(t, expected, output)
}
28 changes: 0 additions & 28 deletions core/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,34 +513,6 @@ func (m *KeyedMutex) LockInt64(key int64) func() {
return mtx.Unlock
}

// BoxOutput formats its arguments as fmt.Printf, and encloses them in a box of
// arrows pointing at their content, in order to better highlight it. See
// ExampleBoxOutput
func BoxOutput(errorMsgTemplate string, errorMsgValues ...interface{}) string {
errorMsgTemplate = fmt.Sprintf(errorMsgTemplate, errorMsgValues...)
lines := strings.Split(errorMsgTemplate, "\n")
maxlen := 0
for _, line := range lines {
if len(line) > maxlen {
maxlen = len(line)
}
}
internalLength := maxlen + 4
output := "↘" + strings.Repeat("↓", internalLength) + "↙\n" // top line
output += "→ " + strings.Repeat(" ", maxlen) + " ←\n"
readme := strings.Repeat("README ", maxlen/7)
output += "→ " + readme + strings.Repeat(" ", maxlen-len(readme)) + " ←\n"
output += "→ " + strings.Repeat(" ", maxlen) + " ←\n"
for _, line := range lines {
output += "→ " + line + strings.Repeat(" ", maxlen-len(line)) + " ←\n"
}
output += "→ " + strings.Repeat(" ", maxlen) + " ←\n"
output += "→ " + readme + strings.Repeat(" ", maxlen-len(readme)) + " ←\n"
output += "→ " + strings.Repeat(" ", maxlen) + " ←\n"
return "\n" + output + "↗" + strings.Repeat("↑", internalLength) + "↖" + // bottom line
"\n\n"
}

// ConcatBytes appends a bunch of byte arrays into a single byte array
func ConcatBytes(bufs ...[]byte) []byte {
return bytes.Join(bufs, []byte{})
Expand Down
18 changes: 0 additions & 18 deletions core/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,24 +205,6 @@ func TestHashPassword(t *testing.T) {
assert.False(t, ok)
}

func TestBoxOutput(t *testing.T) {
t.Parallel()

output := utils.BoxOutput("some error %d %s", 123, "foo")
const expected = "\n" +
"↘↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↙\n" +
"→ ←\n" +
"→ README README ←\n" +
"→ ←\n" +
"→ some error 123 foo ←\n" +
"→ ←\n" +
"→ README README ←\n" +
"→ ←\n" +
"↗↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↖\n" +
"\n"
assert.Equal(t, expected, output)
}

func TestISO8601UTC(t *testing.T) {
t.Parallel()

Expand Down
Loading