Skip to content

Commit

Permalink
hgext/*-format: run mach through python and add more Python varia…
Browse files Browse the repository at this point in the history
…nts (Bug 1755535) r=mhentges

Updates `find_python` to check for three different Python variants,
`py`, `python3` and `python` using `which` and executing `mach` via
whichever is found first. Fall back to `python` if the other variants
can't be found.

Previous tests of these extensions assumed `mach` was runnable as a shell
script and checked a mock-`mach` script into the test repository. Re-write
this script to a Python script to conform to real-`mach`. Changing
the script checked into the initial commit in the test repository means
all commit hashes will change, so update them accordingly.

Differential Revision: https://phabricator.services.mozilla.com/D138846

--HG--
extra : moz-landing-system : lando
  • Loading branch information
cgsheeh committed Mar 2, 2022
1 parent 9174c0e commit 83541dc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 64 deletions.
18 changes: 10 additions & 8 deletions hgext/clang-format/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
"""

import os
import shutil
import subprocess
import sys

from mercurial import (
cmdutil,
error,
extensions,
localrepo,
match,
Expand All @@ -34,7 +35,11 @@
buglink = b'https://bugzilla.mozilla.org/enter_bug.cgi?product=Firefox%20Build%20System&component=Lint%20and%20Formatting' # noqa: E501

def find_python():
return pycompat.bytestr(sys.executable) if sys.version_info[0] >= 3 else b"python3"
for python_variant in (b"py", b"python3", b"python"):
if shutil.which(python_variant):
return python_variant

raise error.Abort(b"Could not find a suitable Python to run `mach`!")


def call_clang_format(repo, changed_files):
Expand All @@ -55,12 +60,9 @@ def call_clang_format(repo, changed_files):
# No files have been touched
return

mach_path = os.path.join(repo.root, b'mach')
arguments = [b'clang-format', b'-p'] + path_list
if os.name == 'nt':
clang_format_cmd = [find_python(), b'mach'] + arguments
else:
clang_format_cmd = [mach_path] + arguments
clang_format_cmd = [
find_python(), os.path.join(repo.root, b'mach'), b'clang-format', b'-p'
] + path_list

# Set `PYTHONIOENCODING` since `hg.exe` will detect `cp1252` as the encoding
# and pass it as the encoding to `mach` via the environment.
Expand Down
50 changes: 26 additions & 24 deletions hgext/clang-format/tests/test-clang-format.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ Create the fake mach
It will take the file and add something at the end of the file

$ cat > mach << EOF
> #!/bin/sh
> filename=\$3
> echo "Reformatting '\$filename'"
> echo 'int clang_format=42;' >> \$filename
> import sys
> filename = sys.argv[-1]
> print("Reformatting '%s'" % filename)
> with open(filename, 'a') as f:
> f.write('int clang_format=42;\n')
>
> EOF
$ chmod +x mach
$ hg add mach
Expand All @@ -40,7 +42,7 @@ Configure the hook
Test the clang-format hook
$ touch foo.cpp
$ ./mach clang-format -p foo.cpp
$ python mach clang-format -p foo.cpp
Reformatting 'foo.cpp'
$ grep -q "int clang_format=42" foo.cpp
$ rm foo.cpp
Expand Down Expand Up @@ -69,32 +71,32 @@ Rebase (should not run the hook)
Reformatting 'dir-2/bar.cpp'
$ grep -q "int clang_format=42" dir-2/bar.cpp
$ hg log --graph
@ changeset: 3:059cd3ee4b4d
@ changeset: 3:423309da9f44
| tag: tip
| user: test
| date: Thu Jan 01 00:00:00 1970 +0000
| summary: third commit
|
o changeset: 2:0088d44a4e42
o changeset: 2:94f859340388
| user: test
| date: Thu Jan 01 00:00:00 1970 +0000
| summary: second commit
|
o changeset: 1:a23f48517f2e
o changeset: 1:ba0c923d4430
| user: test
| date: Thu Jan 01 00:00:00 1970 +0000
| summary: Add bar.cpp before the hook is set
|
o changeset: 0:7ee4c4e22a8d
o changeset: 0:e9bf9f146b90
user: test
date: Thu Jan 01 00:00:00 1970 +0000
summary: Add mach
$ hg rebase -s 2 -d 0
rebasing 2:0088d44a4e42 "second commit"
rebasing 3:059cd3ee4b4d tip "third commit" (hg59 !)
rebasing 3:059cd3ee4b4d "third commit" (tip) (no-hg59 !)
saved backup bundle to $TESTTMP/repo1/.hg/strip-backup/0088d44a4e42-bbfa5a85-rebase.hg
rebasing 2:94f859340388 "second commit"
rebasing 3:423309da9f44 tip "third commit" (hg59 !)
rebasing 3:423309da9f44 "third commit" (tip) (no-hg59 !)
saved backup bundle to $TESTTMP/repo1/.hg/strip-backup/94f859340388-a6dc420b-rebase.hg
$ hg export -r 1 | grep -v -q "int clang_format=42"
$ hg export -r 2 | grep -q "int clang_format=42"
$ hg export -r 3 | grep -q "int clang_format=42"
Expand All @@ -106,40 +108,40 @@ Update (should not run the hook)
$ hg export -r 1 | grep -v -q "int clang_format=42"
$ hg export -r 2 | grep -q "int clang_format=42"
$ hg rebase -s 1 -d 3
rebasing 1:a23f48517f2e "Add bar.cpp before the hook is set"
saved backup bundle to $TESTTMP/repo1/.hg/strip-backup/a23f48517f2e-a25c03d1-rebase.hg
rebasing 1:ba0c923d4430 "Add bar.cpp before the hook is set"
saved backup bundle to $TESTTMP/repo1/.hg/strip-backup/ba0c923d4430-cefd6852-rebase.hg
$ hg update -r tip
2 files updated, 0 files merged, 0 files removed, 0 files unresolved
Histedit
$ echo "histedit =" >> $HGRCPATH
$ cat > histeditcommands << EOF
> mess 866d807fd982 3
> pick fdf8beeead16 2
> pick 7ee4c4e22a8d 0
> pick bc49f9b033d1 1
> mess ba4e12a3249d 3
> pick 7f98a115cbbc 2
> pick e9bf9f146b90 0
> pick e1bd65ca6d5f 1
> EOF
$ hg histedit --commands histeditcommands
saved backup bundle to $TESTTMP/repo1/.hg/strip-backup/7ee4c4e22a8d-1462040c-histedit.hg
saved backup bundle to $TESTTMP/repo1/.hg/strip-backup/e9bf9f146b90-3976ffbc-histedit.hg
$ hg log --graph
@ changeset: 3:72c566e9cf17
@ changeset: 3:44ee0173f95e
| tag: tip
| user: test
| date: Thu Jan 01 00:00:00 1970 +0000
| summary: second commit
|
o changeset: 2:cfc3e492707a
o changeset: 2:36d2eb569efd
| user: test
| date: Thu Jan 01 00:00:00 1970 +0000
| summary: Add mach
|
o changeset: 1:77a3ec17cbe9
o changeset: 1:6f3da0e785ee
| user: test
| date: Thu Jan 01 00:00:00 1970 +0000
| summary: third commit
|
o changeset: 0:3e55a2746795
o changeset: 0:5c9f66f821de
user: test
date: Thu Jan 01 00:00:00 1970 +0000
summary: Add bar.cpp before the hook is set
Expand Down
18 changes: 10 additions & 8 deletions hgext/js-format/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
"""

import os
import shutil
import subprocess
import sys

from mercurial import (
cmdutil,
error,
extensions,
localrepo,
match,
Expand All @@ -34,7 +35,11 @@
buglink = b'https://bugzilla.mozilla.org/enter_bug.cgi?product=Firefox%20Build%20System&component=Lint%20and%20Formatting' # noqa: E501

def find_python():
return pycompat.bytestr(sys.executable) if sys.version_info[0] >= 3 else b"python3"
for python_variant in (b"py", b"python3", b"python"):
if shutil.which(python_variant):
return python_variant

raise error.Abort(b"Could not find a suitable Python to run `mach`!")


def call_js_format(repo, changed_files):
Expand All @@ -50,12 +55,9 @@ def call_js_format(repo, changed_files):
# No files have been touched
return

mach_path = os.path.join(repo.root, b'mach')
arguments = [b'eslint', b'--fix'] + path_list
if os.name == 'nt':
js_format_cmd = [find_python(), b'mach'] + arguments
else:
js_format_cmd = [mach_path] + arguments
js_format_cmd = [
find_python(), os.path.join(repo.root, b'mach'), b'eslint', b'--fix'
] + path_list

# Set `PYTHONIOENCODING` since `hg.exe` will detect `cp1252` as the encoding
# and pass it as the encoding to `mach` via the environment.
Expand Down
50 changes: 26 additions & 24 deletions hgext/js-format/tests/test-js-format.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ Create the fake mach
It will take the file and add something at the end of the file

$ cat > mach << EOF
> #!/bin/sh
> filename=\$3
> echo "Reformatting '\$filename'"
> echo 'let js_format=42;' >> \$filename
> import sys
> filename = sys.argv[-1]
> print("Reformatting '%s'" % filename)
> with open(filename, 'a') as f:
> f.write('let js_format=42;\n')
>
> EOF
$ chmod +x mach
$ hg add mach
Expand All @@ -40,7 +42,7 @@ Configure the hook
Test the js-format hook
$ touch foo.js
$ ./mach eslint --fix foo.js
$ python mach eslint --fix foo.js
Reformatting 'foo.js'
$ grep -q "let js_format=42" foo.js
$ rm foo.js
Expand Down Expand Up @@ -69,32 +71,32 @@ Rebase (should not run the hook)
Reformatting 'dir-2/bar.js'
$ grep -q "let js_format=42" dir-2/bar.js
$ hg log --graph
@ changeset: 3:204033503640
@ changeset: 3:90e129020fa7
| tag: tip
| user: test
| date: Thu Jan 01 00:00:00 1970 +0000
| summary: third commit
|
o changeset: 2:af87cd2e6ed2
o changeset: 2:9b431be05fe2
| user: test
| date: Thu Jan 01 00:00:00 1970 +0000
| summary: second commit
|
o changeset: 1:8bd73e1c68d1
o changeset: 1:35241f108edc
| user: test
| date: Thu Jan 01 00:00:00 1970 +0000
| summary: Add bar.js before the hook is set
|
o changeset: 0:9533808dc03a
o changeset: 0:6ba776e40796
user: test
date: Thu Jan 01 00:00:00 1970 +0000
summary: Add mach
$ hg rebase -s 2 -d 0
rebasing 2:af87cd2e6ed2 "second commit"
rebasing 3:204033503640 tip "third commit" (hg59 !)
rebasing 3:204033503640 "third commit" (tip) (no-hg59 !)
saved backup bundle to $TESTTMP/repo1/.hg/strip-backup/af87cd2e6ed2-4eae2e22-rebase.hg
rebasing 2:9b431be05fe2 "second commit"
rebasing 3:90e129020fa7 tip "third commit" (hg59 !)
rebasing 3:90e129020fa7 "third commit" (tip) (no-hg59 !)
saved backup bundle to $TESTTMP/repo1/.hg/strip-backup/9b431be05fe2-58db80a5-rebase.hg
$ hg export -r 1 | grep -v -q "let js_format=42"
$ hg export -r 2 | grep -q "let js_format=42"
$ hg export -r 3 | grep -q "let js_format=42"
Expand All @@ -106,40 +108,40 @@ Update (should not run the hook)
$ hg export -r 1 | grep -v -q "let js_format=42"
$ hg export -r 2 | grep -q "let js_format=42"
$ hg rebase -s 1 -d 3
rebasing 1:8bd73e1c68d1 "Add bar.js before the hook is set"
saved backup bundle to $TESTTMP/repo1/.hg/strip-backup/8bd73e1c68d1-4e7d2c14-rebase.hg
rebasing 1:35241f108edc "Add bar.js before the hook is set"
saved backup bundle to $TESTTMP/repo1/.hg/strip-backup/35241f108edc-93e75ffd-rebase.hg
$ hg update -r tip
2 files updated, 0 files merged, 0 files removed, 0 files unresolved
Histedit
$ echo "histedit =" >> $HGRCPATH
$ cat > histeditcommands << EOF
> mess b0267487d4b7 3
> pick 739da735cf64 2
> pick 9533808dc03a 0
> pick 98dc618a2a76 1
> mess c0be2ad63b31 3
> pick b4f546d348ab 2
> pick 6ba776e40796 0
> pick 47ccb8c84472 1
> EOF
$ hg histedit --commands histeditcommands
saved backup bundle to $TESTTMP/repo1/.hg/strip-backup/9533808dc03a-8d60e6b6-histedit.hg
saved backup bundle to $TESTTMP/repo1/.hg/strip-backup/6ba776e40796-62ae6ca4-histedit.hg
$ hg log --graph
@ changeset: 3:24829f7ea929
@ changeset: 3:2fd8827288c4
| tag: tip
| user: test
| date: Thu Jan 01 00:00:00 1970 +0000
| summary: second commit
|
o changeset: 2:8fd8c0250e2b
o changeset: 2:ee9d92c29808
| user: test
| date: Thu Jan 01 00:00:00 1970 +0000
| summary: Add mach
|
o changeset: 1:e3754d0980d3
o changeset: 1:fae7b6a64e0f
| user: test
| date: Thu Jan 01 00:00:00 1970 +0000
| summary: third commit
|
o changeset: 0:e57c97e90ee4
o changeset: 0:a4f4d7995acf
user: test
date: Thu Jan 01 00:00:00 1970 +0000
summary: Add bar.js before the hook is set
Expand Down

0 comments on commit 83541dc

Please sign in to comment.