tests/printenv.py
author Kyle Lippincott <spectral@google.com>
Wed, 27 Jan 2021 10:29:21 -0800
branchstable
changeset 46415 8deab876fb59
parent 45830 c102b704edb5
child 48875 6000f5b25c9b
permissions -rwxr-xr-x
wix: tell ComponentSearch that it is finding a directory (not a file) This is to fix an issue we've noticed where fresh installations start at `C:\Program Files\Mercurial`, and then upgrades "walk up" the tree and end up in `C:\Program Files` and finally `C:\` (where they stay). ComponentSearch defaults to finding files, which I think means "it produces a string like `C:\Program Files\Mercurial`", whereas with the type being explicitly a directory, it would return `C:\Program Files\Mercurial\` (note the final trailing backslash). Presumably, a latter step then tries to turn that file name into a proper directory, by removing everything after the last `\`. This could likely also be fixed by actually searching for the component for hg.exe itself. That seemed a lot more complicated, as the GUID for hg.exe isn't known in this file (it's one of the "auto-derived" ones). We could also consider adding a Condition that I think could check the Property and ensure it's either empty or ends in a trailing slash, but that would be an installer runtime check and I'm not convinced it'd actually be useful. This will *not* cause existing installations that are in one of the bad directories to fix themselves. Doing that would require a fair amount more understanding of wix and windows installer than I have, and it *probably* wouldn't be possible to be 100% correct about it either (there's nothing preventing a user from intentionally installing it in C:\, though I don't know why they would do so). If someone wants to tackle fixing existing installations, I think that the first installation is actually the only one that shows up in "Add or Remove Programs", and that its registry keys still exist. You might be able to find something under HKEY_USERS that lists both the "good" and the "bad" InstallDirs. Mine was under `HKEY_USERS\S-1-5-18\Software\Mercurial\InstallDir` (C:\), and `HKEY_USERS\S-1-5-21-..numbers..\Software\Mercurial\InstallDir` (C:\Program Files\Mercurial). If you find exactly two, with one being the default path, and the other being a prefix of it, the user almost certainly hit this bug :D We had originally thought that this bug might be due to unattended installations/upgrades, but I no longer think that's the case. We were able to reproduce the issue by uninstalling all copies of Mercurial I could find, installing one version (it chose the correct location), and then starting the installer for a different version (higher or lower didn't matter). I did not need to deal with an unattended or headless installation/upgrade to trigger the issue, but it's possible that my system was "primed" for this bug to happen because of a previous unattended installation/upgrade. Differential Revision: https://phab.mercurial-scm.org/D9891
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45830
c102b704edb5 global: use python3 in shebangs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
     1
#!/usr/bin/env python3
25477
a372f7b4463b tests: make printenv executable
Matt Mackall <mpm@selenic.com>
parents: 17018
diff changeset
     2
#
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     3
# simple script to be used in hooks
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     4
#
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     5
# put something like this in the repo .hg/hgrc:
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     6
#
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     7
#     [hooks]
17018
e7fdfc702d9f tests: consistently use printenv.py the same MSYS/Windows-compatible way
Mads Kiilerich <mads@kiilerich.com>
parents: 16982
diff changeset
     8
#     changegroup = python "$TESTDIR/printenv.py" <hookname> [exit] [output]
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     9
#
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    10
#   - <hookname> is a mandatory argument (e.g. "changegroup")
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    11
#   - [exit] is the exit code of the hook (default: 0)
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    12
#   - [output] is the name of the output file (default: use sys.stdout)
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    13
#              the file will be opened in append mode.
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    14
#
28944
036787c10b16 py3: use absolute_import in printenv.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 25477
diff changeset
    15
from __future__ import absolute_import
41025
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    16
import argparse
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    17
import os
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    18
import sys
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    19
7080
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
    20
try:
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
    21
    import msvcrt
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41036
diff changeset
    22
7080
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
    23
    msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
    24
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
7186
f77c8d8331ca clean up trailing spaces, leading spaces in C
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7080
diff changeset
    25
    msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
7080
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
    26
except ImportError:
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
    27
    pass
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
    28
41025
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    29
parser = argparse.ArgumentParser()
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    30
parser.add_argument("name", help="the hook name, used for display")
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    31
parser.add_argument(
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    32
    "exitcode",
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    33
    nargs="?",
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    34
    default=0,
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    35
    type=int,
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    36
    help="the exit code for the hook",
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    37
)
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    38
parser.add_argument(
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    39
    "out", nargs="?", default=None, help="where to write the output"
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    40
)
41036
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    41
parser.add_argument(
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    42
    "--line",
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    43
    action="store_true",
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    44
    help="print environment variables one per line instead of on a single line",
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    45
)
41025
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    46
args = parser.parse_args()
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    47
41025
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    48
if args.out is None:
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    49
    out = sys.stdout
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    50
    out = getattr(out, "buffer", out)
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    51
else:
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    52
    out = open(args.out, "ab")
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    53
4643
a39cec1d5cb8 printenv: filter empty environment variables for portability.
Patrick Mezard <pmezard@gmail.com>
parents: 4285
diff changeset
    54
# variables with empty values may not exist on all platforms, filter
a39cec1d5cb8 printenv: filter empty environment variables for portability.
Patrick Mezard <pmezard@gmail.com>
parents: 4285
diff changeset
    55
# them now for portability sake.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41036
diff changeset
    56
env = [(k, v) for k, v in os.environ.items() if k.startswith("HG_") and v]
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    57
env.sort()
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    58
41025
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    59
out.write(b"%s hook: " % args.name.encode('ascii'))
16982
9c892c830a72 tests/printenv.py: eliminate trailing spaces on output
Adrian Buehlmann <adrian@cadifra.com>
parents: 16963
diff changeset
    60
if os.name == 'nt':
9c892c830a72 tests/printenv.py: eliminate trailing spaces on output
Adrian Buehlmann <adrian@cadifra.com>
parents: 16963
diff changeset
    61
    filter = lambda x: x.replace('\\', '/')
9c892c830a72 tests/printenv.py: eliminate trailing spaces on output
Adrian Buehlmann <adrian@cadifra.com>
parents: 16963
diff changeset
    62
else:
9c892c830a72 tests/printenv.py: eliminate trailing spaces on output
Adrian Buehlmann <adrian@cadifra.com>
parents: 16963
diff changeset
    63
    filter = lambda x: x
41036
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    64
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41036
diff changeset
    65
vars = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41036
diff changeset
    66
    b"%s=%s" % (k.encode('ascii'), filter(v).encode('ascii')) for k, v in env
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41036
diff changeset
    67
]
41036
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    68
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    69
# Print variables on out
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    70
if not args.line:
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    71
    out.write(b" ".join(vars))
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    72
else:
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    73
    for var in vars:
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    74
        out.write(var)
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    75
        out.write(b"\n")
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    76
38111
bacbe829c2bf py3: use bytes in tests/printenv.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36254
diff changeset
    77
out.write(b"\n")
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    78
out.close()
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    79
41025
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    80
sys.exit(args.exitcode)