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...@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.
So:$ cat mypackage.gopackage mypackageimport "something/something/foo"func MyFunction() {some codesome codesome codefoo.Bar(make(chan foo.Baz), make(chan foo.Foobar))}Is the code I would really like to write.foo has build constraints. As you can see, my code doesn't, it's the exact thing you recommended: the common code is in mypackage.go, the platform specific code is in foo (which I don't control, it isn't my code, so I can't change it). How to unit test MyFunction to verify that it uses foo.Bar correctly?
Normally, I would just pass foo.Bar in argument to the function or do something like var fooBar = foo.Bar so I can replace it in unit tests.
But in the case of a foo package that has platform build constraints, I don't see how it's possible without creating a wrapper.On Wednesday, January 5, 2022 at 10:20:47 AM UTC Jan Mercl wrote:On Wed, Jan 5, 2022 at 11:09 AM Brieuc Jeunhomme <bjeun...@gmail.com> wrote:
> 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.
I don't get it. Put the common code in a file/in files with no build
constraints and the target specific bits in files with build
constraints for the target. What wrappers are needed and why?
Package level declarations are visible in all files within a package -
meaning they have package scope. The sole exception is the package
qualifier explicitly or implicitly declared by an import statement.
Such identifier has file scope.
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/74eed2b8-9a6a-4ad1-a30a-9ceca71e5bc8n%40googlegroups.com.