Colcon package-of

Announcing a small Colcon extension:

pip install colcon-package-of

Given a set of files, output a list of packages which contain those files.

$ colcon package-of src/pkgX/foo.txt src/pkgY/bar.txt
pkgX    src/pkgX    (python)
pkgY    src/pkgY    (python)

The --names-only and --paths-only options can be used to adjust the output.

For convince, there is also a --git-diff option, which is basically equivalent
to using git diff with xargs. If there are added, deleted, modified, or
untracked files corresponding to a package, that package will be listed:

$ touch src/pkgA/newfoo.txt
$ rm src/pkgB/oldbar.txt
$ colcon package-of --git-diff HEAD
pkgA    src/pkgA    (python)
pkgB    src/pkgB    (python)

The intention is for this to be useful as part of a CI pipeline, to build only the packages that have changed since the last commit, for example:

DIFF_PKGS=$(colcon package-of -n --git-diff HEAD^)
[ -z "$DIFF_PKGS" ] || colcon build --packages-above $DIFF_PKGS

Please let me know what you think!

5 Likes

By the way, I think the way I test this extension is kind of neat. When I looked at existing Colcon projects, they seemed to only have some unit tests, but nothing to test the actual command line behavior. I also realized it is pretty difficult to write a unit test for a verb. (Actually, I wasn’t able to figure it out.)

I found a tool called Prysk that could be used to test a command line interface. The tests basically look like a transcript from a terminal session. Here’s an example of a test written for Prysk. It was not too difficult. The tricky part was setting up Colcon and the extension in a virtual environment, plus the mock Colon workspace to test against. I am pretty happy with the result, and it did help catch a bug I had missed before.

1 Like