Yes, that's what I wanted to do, but it looks very tedious at best.  In 
"normal" unit tests, it's simple: import the package you're using, inject 
its functions, reuse its types and constants.

But here ,since under non-windows platforms, I can't import the package, it 
means I have to define wrappers for everything I use: types, constants, 
functions.  And when a function gets a channel in argument, the wrapper is 
much more than a trivial one liner, it often needs a goroutine.

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.  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.

Reply via email to