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

fix: Avoid ParseBool error #2011

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

machupicchubeta
Copy link

Summary

The error occurs if I set "yes" to FORCE_PREPEND env as described in the documentation.

Error message

strconv.ParseBool: parsing \"yes\": invalid syntax

Other Information

Referenced Documents

@machupicchubeta machupicchubeta requested a review from a team as a code owner March 9, 2025 15:19
The error occurs if I set "yes" to `FORCE_PREPEND` env as described in the documentation.

Error message:
```
strconv.ParseBool: parsing \"yes\": invalid syntax
```

Referenced Documents:
- https://github.com/asdf-vm/asdf/blob/7352bf4890143184ba419392ca5e6204167c8306/docs/manage/configuration.md#asdf_force_prepend

assert.Nil(t, err, "Returned error when loading env for config")

assert.Zero(t, config.Home, "Shouldn't set Home property when loading config")
Copy link
Contributor

@andrecloutier andrecloutier Mar 9, 2025

Choose a reason for hiding this comment

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

This test should be updated to check whether ForcePrepend was actually set to the correct/expected value. Also consider adding a test for the default case (where ASDF_FORCE_PREPEND is not set).

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your comment.
I have changed the test code to check that ForcePrepend is set the correct/expected value.
And I have check the initial value when ASDF_FORCE_PREPEND is not set.

942578e

Copy link
Author

Choose a reason for hiding this comment

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

@andrecloutier
By updating the test cases, I was able to notice where there were discrepancies with the specifications, and I updated the code to include those as well.
Would you please code review again?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looking good! Added a few minor comments.

By the way... From searching the code, it looks like nothing is reading from ForcePrepend. So, while this PR will fix the parsing of the value, this won't actually result in any visible change in behavior in the application.

Copy link
Author

Choose a reason for hiding this comment

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

@andrecloutier
Thank you so much.
There is nothing is reading ForcePrepend .... Oh, indeed. I noticed that when you mentioned it.
I will consider that as another issue.

The reason I decided to write this code in the first place was because I saw the error on my local terminal.
For some reason I can't reproduce it now (.... hmm), but I hope to be able to prevent similar case.

…ironment variable

Those test cases aims to check whether `ForcePrepend` was actually set to the correct/expected value.

Referenced Documents:
- https://github.com/asdf-vm/asdf/blob/7352bf4890143184ba419392ca5e6204167c8306/docs/manage/configuration.md#asdf_force_prepend
@machupicchubeta machupicchubeta force-pushed the force_prepend branch 3 times, most recently from 559004e to b017252 Compare March 10, 2025 15:21
If the `ASDF_FORCE_PREPEND` environment variable is not set on macOS, the behavior is the same as when "yes" is set.

Referenced Documents:
- https://github.com/asdf-vm/asdf/blob/7352bf4890143184ba419392ca5e6204167c8306/docs/manage/configuration.md#asdf_force_prepend
`ASDF_FORCE_PREPEND` is a specification that changes behavior depending on whether the string is "yes" or not.

Referenced Documents:
- https://github.com/asdf-vm/asdf/blob/7352bf4890143184ba419392ca5e6204167c8306/docs/manage/configuration.md#asdf_force_prepend
forcePrepend = true
default:
forcePrepend = false
}
Copy link
Contributor

@andrecloutier andrecloutier Mar 11, 2025

Choose a reason for hiding this comment

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

I think the use of switch here adds unnecessary complexity and branches to consider. I suggest using an if instead:

	forcePrepend := forcePrependDefault
	forcePrependEnv := os.Getenv("ASDF_FORCE_PREPEND")
	if forcePrependEnv == "yes" || (forcePrependEnv == "" && runtime.GOOS == "darwin") {
		forcePrepend = true
	}

Copy link
Author

Choose a reason for hiding this comment

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

Yes. that's exactly it!
Thank you!

d76ef9b

func TestLoadConfigEnv_WithForcePrependEnv(t *testing.T) {
t.Run("When ASDF_FORCE_PREPEND env given yes", func(t *testing.T) {
os.Setenv("ASDF_FORCE_PREPEND", "yes")
defer os.Unsetenv("ASDF_FORCE_PREPEND")
Copy link
Contributor

Choose a reason for hiding this comment

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

Using t.SetEnv in a substitute to having to do any special defer handling. (In fact, there's a bug here where ASDF_FORCE_PREPEND should be set back to the previous value if it were set to one before).

Copy link
Author

Choose a reason for hiding this comment

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

I see.
Thank you very much!
I fix the code.

a930763


t.Run("When ASDF_FORCE_PREPEND env given any string other than yes", func(t *testing.T) {
os.Setenv("ASDF_FORCE_PREPEND", "no")
defer os.Unsetenv("ASDF_FORCE_PREPEND")
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment here.

Copy link
Author

Choose a reason for hiding this comment

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

It is the same as #2011 (comment).

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