3

In a Dist::Zilla-based distribution I would like to have some files that are only used for testing, but do not get installed. These are mockup libs that aren't needed for runtime.

How do I do that?

simone
  • 4,667
  • 4
  • 25
  • 47

2 Answers2

5

CPAN distributions never install the t and xt directories. You can put your tests and your mock libs into t.

As an example, take my module MooseX::LocalAttribute. In the dist, there is a t/, a t/lib and an xt/.

If you install this using cpanm -l into a local lib dir, you will see there are no tests installed. This happens automatically. It's just how CPAN works.

$ cpanm -l mylib MooseX::LocalAttribute
--> Working on MooseX::LocalAttribute
Fetching http://www.cpan.org/authors/id/S/SI/SIMBABQUE/MooseX-LocalAttribute-0.05.tar.gz ... OK
Configuring MooseX-LocalAttribute-0.05 ... OK
Building and testing MooseX-LocalAttribute-0.05 ... OK
Successfully installed MooseX-LocalAttribute-0.05
1 distribution installed

$ tree mylib
mylib
├── lib
│   └── perl5
│       ├── MooseX
│       │   └── LocalAttribute.pm
│       └── x86_64-linux
│           ├── auto
│           │   └── MooseX
│           │       └── LocalAttribute
│           └── perllocal.pod
└── man
    └── man3
        └── MooseX::LocalAttribute.3

9 directories, 3 files

Note that as long as stuff is in t/lib (or anywhere under t/, really), you do not have to hide the package names from the PAUSE indexer. It's smart enough to not find it.

simbabque
  • 53,749
  • 8
  • 73
  • 136
2

I misunderstood the question. This answer is for the following question:

How do I exclude files from a Dist::Zilla based distribution so they don't get shipped at all?

You are probably using either the GatherDir or Git::GatherDir plugin to build your bundle. Both of them have an option exclude_filename that you can set in your dist.ini to not include a file in a bundle.

A common pattern is to exclude auto-generated files such as LICENSE or META.json, and then add them later with another plugin. But you don't have to do that, you can just exclude files completely.

A good example is the URI distribution. On metacpan, it does not include any text files in the bundle. But if you look at the repository on github, you can see there are various .txt files such as rfc2396.txt. The dist.ini contains the following lines.

[Git::GatherDir]
exclude_filename = LICENSE
exclude_filename = README.md
exclude_filename = draft-duerst-iri-bis.txt
exclude_filename = rfc2396.txt
exclude_filename = rfc3986.txt
exclude_filename = rfc3987.txt

As mentioned before, the LICENSE and README.md files will still appear in the final bundle, because they get added later via @Git::VersionManager.

simbabque
  • 53,749
  • 8
  • 73
  • 136
  • Thanks - but does this mean that the files will not be installed (I.e.: that they will only be used for building and testing) or that they won’t be part of the distribution at all? I understand that excluding results in the latter not the former. Did I get it wrong? Sorry - it’s about the third dist I build and I’m only getting into testing – simone Sep 05 '22 at 11:09
  • @simone oh. I think I misunderstood. Did you mean the `t/` directory were your unit tests live? That never gets installed. `t/` and `xt/` don't get copied by the cpan (or cpanm, or whatvever) clients. Try to install any CPAN module with `cpanm -l mylib `, then `tree mylib`. You'll see there are no tests. – simbabque Sep 05 '22 at 11:13
  • I am going to add a second answer detailing this. – simbabque Sep 05 '22 at 11:18
  • Caveat: I’m a total newbie to testing. In my module I have a DBIx::Class schema for a database, which is only there for testing. It’s sits under ./lib but I obviously don’t want it installed. How do I deal with that? Should I maybe have a ./t/lib directory? – simone Sep 05 '22 at 11:27
  • @simone if you want to have a chat, drop me an email. I might be able to explain some concepts. :) – simbabque Sep 05 '22 at 11:30
  • thanks, where do I find the address? – simone Sep 05 '22 at 12:23
  • @simone look for something I've released to CPAN :) – simbabque Sep 05 '22 at 14:09