<div dir="ltr">John talked to me on a hangout and suggested that, often times, when you need to expose something like this for tests, it ends up being something that production code needs to tweak as well. He'd like to see something better than a public variable, but didn't offer a suggestion as to what.<div>
<br></div><div>I think this is a pretty good point, so I'll try to come up with a third way that isn't too cumbersome.</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Mar 3, 2014 at 12:35 PM, Nate Finch <span dir="ltr"><<a href="mailto:nate.finch@canonical.com" target="_blank">nate.finch@canonical.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">In some places, we have exposed constants that should not normally be public as public variables, purely for the purpose of testing. The reason we need to do that, is because packages can't use code that is only exposed in _test files for other packages (i.e. package foo has a export_test.go that it uses to export wrappers for internal things to be tweaked during testing. However, package bar can't access the stuff in foo's export_test.go, only foo can access that stuff.)<div>
<br></div><div>The problem with these public variables is that the only thing preventing people from using them is a comment that says "this is only for testing, don't modify during production".</div><div><br>
</div><div>If you're consuming the code and see this public name pop up in code completion, you might not realize it's only for testing, and use it anyway.</div><div><br></div><div>I've actually seen and fixed a bug like this, where production code was twiddling with one of these variables that it was not supposed to be using.</div>
<div><br></div><div>The fix I thought up was to instead make a function called MockPackage(). This function does everything that is needed to mock out the package for testing, and returns a function that undoes the mocking.</div>
<div><br></div><div>The reason I like this better that public variables is twofold:</div><div><br></div><div>1.) It's a lot more obvious that MockPackage is something that you shouldn't be calling in production code... rather than some vaguely named public variable that may or may not be one of these special "do not touch" variables. This is doubly true if MockPackage becomes a standard pattern.</div>
<div><br></div><div>2.) It abstracts away the mocking of this package from consumers of the package, so that if the package changes and needs to modify how it mocks out its functionality, you don't have to change any other code.</div>
<div><br></div><div>I used this pattern in a merge proposal, and Roger suggested we just do it the old way, with a public variable... so I'd like more input from the team.</div><span class="HOEnZb"><font color="#888888"><div>
<br></div><div>-Nate</div></font></span></div>
</blockquote></div><br></div>