Skip to content

Conversation

dennisdegreef
Copy link
Contributor

I sometimes use the GNUPGHOME variable to point to a different set of rings.

This pull request should enable the usage of the environment variable and perform the previous behaviour if it is not set.
Let me know what you think.

@dennisdegreef
Copy link
Contributor Author

dennisdegreef commented Apr 30, 2016

I'm also looking into writing a test for this use case. Currently I have this:

func (s *LocalPGPTest) TestGnupgHomeOverride() {
    os.Setenv("GNUPGHOME", "/foo")
    _, err := NewLocalPGPService()
    s.True(err.Error() == "stat /foo/.gnupg/pubring.gpg: no such file or directory")
    os.Unsetenv("GNUPGHOME")
}

Which I find a bit ugly, since this asserts on matching an error string, instead of the compiled path.
The problem is that without mocking (or being able to mock?) os.Stat, there is no other way around this as I see it.
I haven't had too much experience with go, so any insight in a proper solution is very welcome.

@dennisdegreef dennisdegreef force-pushed the supportCustomGnupgHome branch from 047f512 to 2a6d5be Compare April 30, 2016 11:00
@ellotheth
Copy link
Owner

Great idea to use $GNUPGHOME. I'll be able to dig into this over the weekend and get you more feedback. Thanks!

home = os.Getenv("GNUPGHOME")
}

ringfile := path.Join(home, ".gnupg", "pubring.gpg")
Copy link
Owner

Choose a reason for hiding this comment

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

If $GNUPGHOME is set, ringfile here would be $GNUPGHOME/.gnupg/pubring.gpg. Is that the standard usage? Reading the gpg man page, it looks like $GNUPGHOME is equivalent to $HOME/.gnupg, i.e. the top-level GnuPG directory:

--homedir dir
Set the name of the home directory to dir. If this option is not used, the home directory defaults to ~/.gnupg. It is only recognized when given on the command line. It also overrides any home directory stated through the environment variable GNUPGHOME

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, I could have tested this. I don't use $GNUPGHOME on the machine I coded this :)
I rebased my branch from master and changed the path. Should be correct now.

@ellotheth
Copy link
Owner

For the test, it might make sense to extract the ringfile composition out to a method that returns the full path, and then test the method instead of the whole constructor. That way you don't have to worry about testing whether the file exists with os.Stat. (Which is really pushing off the inevitable, but at least then it's my problem, not yours!)

@dennisdegreef dennisdegreef force-pushed the supportCustomGnupgHome branch from 2a6d5be to e9995dc Compare May 14, 2016 21:23
@dennisdegreef
Copy link
Contributor Author

dennisdegreef commented May 15, 2016

Hi @ellotheth

I just refactored the ringfile to a separate struct and added/changed tests for multiple use cases.
As for pushing off the os.Stat mocking, I agree that splitting for now is a good solution.

dennisdegreef#1

Let me know what you think, when I merge this, it'll be added in this branch/PR.

@ellotheth
Copy link
Owner

Hi @dennisdegreef,

Your branch looks stellar, but I'm not quite ready to start wrapping os methods to that extent. I had in mind something simpler, like e9995dc...alt-for-gnupghome. It skips dealing with os.Stat entirely, and focuses on building the file path. What do you think?

@dennisdegreef
Copy link
Contributor Author

It looks good! 👍 I was just trying out a different solution, while making myself more familiar with Golang.
Your solution is indeed simpler. I'll cherry-pick that commit in my branch. Thanks.

dennisdegreef pushed a commit to dennisdegreef/pipethis that referenced this pull request Jun 12, 2016
@ellotheth
Copy link
Owner

Now that I've dirtied up master, do you mind rebasing your branch? I'll merge and tag everything afterwards.

@dennisdegreef dennisdegreef force-pushed the supportCustomGnupgHome branch from bbceaa4 to 83f1304 Compare June 18, 2016 18:38
@dennisdegreef
Copy link
Contributor Author

No problem, pushed it

@ellotheth ellotheth merged commit d9eca26 into ellotheth:master Jun 18, 2016
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