3

I'm writing some unit tests that call out to Mercurial, and I'd like to ensure the tests don't rely on any local state of the machine. I know I can override certain config values, e.g.:

hg --config ui.username='Test Account <not-a-real-address@example.com>' ...

However this still reads in the global and user .hgrc files on the machine, which could affect Mercurial's behavior in an unintended or undesirable way.

Is there a robust way to invoke hg without having it look at any config files except the .hg/hgrc file in the repo? For instance with Bash I can accomplish this with bash --noprofile --norc, but I don't see a similar flag for Mercurial.

NB: I'm aware of "heavyweight" options such as running the tests in an isolated virtual environment such as Docker. I'll certainly do something like that if necessary, but I'm wondering if there's a way to configure Mercurial itself.

dimo414
  • 47,227
  • 18
  • 148
  • 244

2 Answers2

3

A bit more poking found Issue 3147: command line option to disable ~/.hgrc, which was CLOSED WONTFIX, however the workaround suggested there appears to work as desired:

$ HGRCPATH= hg ...

This overrides the default search paths for hgrc files, though it isn't mentioned (as of this writing) in the hgrc docs (edit: it's mentioned on another page).

Example:

# lots of inherited config by default
$ hg showconfig | wc
     118     361    7232

$ HGRCPATH= hg showconfig
ui.formatted=True
ui.interactive=False

$ hg init foo && cd foo

$ HGRCPATH= hg showconfig
bundle.mainreporoot=/tmp/foo
ui.formatted=True
ui.interactive=False
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • 2
    dimo414 answer is the good answer. I would add setting `HGPLAIN` also if you plan to read the Mercurial command outputs, this way you will have stable and reproducible outputs. – Boris Feld Oct 01 '18 at 08:02
  • @BorisFeld thanks for the suggestion! Not currently a requirement for my use-case, but good to know about. – dimo414 Oct 01 '18 at 22:06
0

In scripts you usually want to ensure that no local configuration and localisation breaks it. Mercurial provides the environment variable HGPLAIN= for that purpose. Thus in your scripts invoke mercurial always by setting it for that particular invocation of mercurial:

HGPLAIN= hg status

This makes sure that you see the output as produced by vanilla mercurial. See also https://www.mercurial-scm.org/repo/hg/help/environment and how to define exceptions to this rule, if needed.

planetmaker
  • 5,884
  • 3
  • 28
  • 37
  • Thanks for the pointer, but `HGPLAIN= hg showconfig` still shows a number of customizations, such as `ui.username`, that I don't want to include. I think `HGRCPATH` is what I need. – dimo414 Oct 01 '18 at 22:08