OpenPeeDeeP / depguard

Go linter that checks if package imports are in a list of acceptable packages.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to blacklist import in non-test files, but allow them in test files?

marten-seemann opened this issue · comments

I'm using the Ginkgo test framework for unit tests in one of my projects. I want to allow importing of github.com/onsi/ginkgo in all _test.go files, but disallow it in non-test files, so that a build of my production code doesn't pull in a test framework.

I can't figure out how to configure depguard to perform that check. Is it possible?

You will need to setup a whitelist and only put th ginkgo package in the InTest configuration portion of the tool. Unfortunatly the configuration doesn't allow for split list types. So you will need to whitelist all non test packages too.

I believe that if inTests is empty, then whatever is in packages also applies to tests. So, listing anything in inTests makes it work. Something like this. It's ugly, but seems to work.

{
  "type": "blacklist",
  "packages": [
    "github.com/onsi/ginkgo"
  ],                                                                                                                
  "inTests": [
    "this isn't even a proper package name"
  ],  
  "includeGoStdLib": true
}

I realize that this thread is a bit old, but I was having a similar problem and I submitted a Pull Request to allow for partial list types - #21