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

Standardize on only two value dumping/diffing libraries #669

Merged
merged 1 commit into from
Mar 14, 2024

Conversation

apparentlymart
Copy link
Contributor

Due to the quite messy heritage of this codebase -- including a large part of it starting as a fork of my earlier personal project ZCL, which had made different choices -- there were many different conventions for how to pretty-print and diff values in the tests in different parts of the codebase.

To reduce the dependency sprawl, this commit now standardizes on:

  • github.com/davecgh/go-spew for pretty-printing
  • github.com/google/go-cmp for diffing

These two dependencies were already present anyway, are the most general out of all of the candidates, and are also already in use by at least some of HCL's most significant callers, such as HashiCorp Terraform.


Since this potentially affects how the tests report failure, I visited each test I changed and made it intentionally fail to verify that the result is still readable and includes similar information as before, even though some of them are now displaying slightly differently.

This only changes _test.go files, so it cannot affect any real behavior of the library when consumed by other modules.

@apparentlymart apparentlymart requested a review from a team March 12, 2024 22:45
@apparentlymart apparentlymart self-assigned this Mar 12, 2024
@apparentlymart
Copy link
Contributor Author

Hmm interesting... this failed in a strange way when running under GitHub Actions, which doesn't readily reproduce on my own computer. I'm going to make this a draft while I investigate further.

@apparentlymart apparentlymart marked this pull request as draft March 12, 2024 22:47
@apparentlymart
Copy link
Contributor Author

The difference in behavior is because we are running go test -race in the GitHub Actions workflow. I can reproduce it on my system if I also enable the race detector.

Due to the quite messy heritage of this codebase -- including a large part
of it being just a fork of my earlier personal project ZCL -- there were
many different conventions for how to pretty-print and diff values in the
tests in different parts of the codebase.

To reduce the dependency sprawl, this commit now standardizes on:
  - github.com/davecgh/go-spew for pretty-printing
  - github.com/google/go-cmp for diffing

These two dependencies were already present anyway, are the most general
out of all of the candidates, and are also already in use by at least some
of HCL's most significant callers, such as HashiCorp Terraform.

The version of go-cmp we were previously using seems to have a bug that
causes the tests to crash when run under the Go race detector, so I've
also upgraded that dependency to latest here to clear that bug.
@apparentlymart
Copy link
Contributor Author

The failure seems to have been caused by this upstream bug: google/go-cmp#167

Therefore I've modified this change to also upgrade to the latest version of go-cmp, where the bug is fixed and the tests can now pass without crashing under the race detector too.

@apparentlymart apparentlymart marked this pull request as ready for review March 12, 2024 23:00
@@ -2197,6 +2194,7 @@ block "valid" {}
"a": {
Name: "a",
Expr: &LiteralValueExpr{
Val: cty.DynamicVal,
Copy link
Contributor Author

@apparentlymart apparentlymart Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous approach to this test was, it turns out, quietly not actually checking the Val field at all, because cty.Value has only unexported fields.

Switching to cmp brought attention to that first by requiring the use of ctydebug.CmpOptions (which teaches cmp how to compare two cty.Value values) and then by showing failures for this test and the other similar one below.

@apparentlymart apparentlymart merged commit 2a0a3f0 into main Mar 14, 2024
13 checks passed
@apparentlymart apparentlymart deleted the shed-pretty-deps branch March 14, 2024 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants