I'm also confused.

On Wed, Jan 5, 2022 at 11:09 AM Brieuc Jeunhomme <bjeunho...@gmail.com>
wrote:

> For example, let's say you want to unit test MyFunction, which calls
> foo.Bar(chan foo.Baz, chan foo.Foobar) and foo compiles only under a single
> OS.  If you want to write a portable unit test for MyFunction, you can't
> import foo.
>

To me, there are two possibilities:
1. MyFunction is portable, i.e. it contains build-tag guarded
implementations for the other relevant platforms as well, which work
without importing `foo`. In that case, you can just call it as normal, it
doesn't actually matter to its API or test.
2. MyFunction is not portable, it only works on certain platforms. In that
case, why would you need a portable unit test? It can only be called on
platforms `foo` works on, so there is no need to test it on any other
platform.

Obviously, you have to actually *run* the tests on the platforms which are
supported, but that seems a given.


> So you have to wrap everything in foo.  And since you can't have the
> wrappers take a foo.Baz or foo.Foobar argument, the wrapper for foo.Bar
> will be non-trivial, it will require to read/write from both channels,
> convert whatever comes in/out of them into your wrapper type... That's a
> lot of manual work, and it's error prone.  I suppose there's a better way
> to do that than to wrap everything manually?
>
> On Tuesday, January 4, 2022 at 7:51:25 PM UTC kortschak wrote:
>
>> On Tue, 2022-01-04 at 08:29 -0800, Brieuc Jeunhomme wrote:
>> > Hi,
>> >
>> > I'm writing code that uses the golang.org/x/sys/windows/svc package.
>> > This package compiles only for windows builds, for other GOOS values,
>> > I get a build error. That's fair, but I'm wondering how to unit test
>> > the code that calls svc functions when working on a non-windows
>> > environment. So code that looks like:
>> >
>> > func myFunction() {
>> > svc.Foo(make(chan svc.Bar))
>> > }
>> >
>> > Is there a well known way to do that, or am I missing something
>>
>> You can use build constraints, for example `// +build windows` and `//
>> +build !windows` to selectively build the tests only on the desired
>> platform[1].
>>
>>
>> [1]https://pkg.go.dev/cmd/go#hdr-Build_constraints
>>
>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/30f195e2-bc40-468d-aeb7-eb19101a6762n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/30f195e2-bc40-468d-aeb7-eb19101a6762n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfG6UD-MGLX7ibs6E8uPSD576tvQLctW_oV-oJKV8-OXaw%40mail.gmail.com.

Reply via email to