21 Commits

Author SHA1 Message Date
Joel Brobecker
8821aec0fd Force the use of quoted-printable as the Content-Transfer-Encoding
This commit changes the hooks to always use the quoted-printable
encoding when sending emails. This replaces the use of 7bit, 8bit
and base64 encodings. A comment is added explaining the reasons
for choosing this encoding.

For the record, the expected output in our testsuite has been
adjusted automatically using the following Python script...

    | #! /usr/bin/env python3
    |
    | import argparse
    |
    | def fixup(filename):
    |     with open(filename) as f:
    |         contents = f.read()
    |
    |     new_contents = contents.replace(
    |         """\
    | remote: DEBUG: MIME-Version: 1.0
    | remote: Content-Transfer-Encoding: 7bit
    | remote: Content-Type: text/plain; charset="utf-8"
    | """,
    |         """\
    | remote: DEBUG: Content-Type: text/plain; charset="utf-8"
    | remote: MIME-Version: 1.0
    | remote: Content-Transfer-Encoding: quoted-printable
    | """,
    |     )
    |
    |     new_contents = new_contents.replace(
    |         "remote: Content-Transfer-Encoding: base64",
    |         "remote: Content-Transfer-Encoding: quoted-printable",
    |     )
    |
    |     if new_contents == contents:
    |         return
    |
    |     with open(filename, "w") as f:
    |         f.write(new_contents)
    |
    |
    | parser = argparse.ArgumentParser()
    | parser.add_argument("files_list", nargs="+")
    | args = parser.parse_args()
    |
    | for filename in args.files_list:
    |     fixup(filename)

... calling it as follow:

    $ cd testsuite/tests
    $ /path/to/myscript.py */run_test.py

I think a perl oneliner was also possible, but I couldn't get it
to work (multiline issue, I think).

This still left one testcase that needed adjustement
(post-receive_from_email); I just manually adjusted it.

Change-Id: I8455d24f8fe0b9a732c68090a21091db060e03f2
TN: TB22-001
2022-01-04 07:54:02 +00:00
Joel Brobecker
8b5d28174e Remove all imports of __future__.print_functions in the testsuite
This commit is the result of running the following command in
the testsuite/ directory:

   $ sed -i '/^from\s\+__future__\s\+import\s\+print_function\s*$/d' \
       `git grep print_function | cut -d: -f1 | sort -u`

Change-Id: Idb18a24f5368434a505af42b347b8f2a1d0481b5
TN: U530-006
2021-11-30 17:58:55 +04:00
Joel Brobecker
6f3ee523bf Port the testsuite to pytest and Python 3.x
This commit introduces an entirely new testsuite framework based on
pytest, and then converts all our existing testcases to this new
framework.

Note: One helpful way to review this patch is to use "git show -w";
      For the most part, this patch just changes the indentation level
      of existing code. By asking "git show" to hide changes which only
      involve changes in spaces, the reviewer is left with the "essence"
      of the change.

Note: There are some testcases where the content transfer encoding changes
      from 8bit to base64. This is an enhancement brought by using a more
      recent version of Python, and in particular of the email support
      classes it provides.

Note: There are commits where the "[Errno 2] No such file or directory"
      error message now includes the path that's missing. E.g:
        - cannot_find_style_checker
        - update_hook_not_found

Still left to do:

  - Remove the old testsuite framework;
  - Remove the py2-only code;
  - Remove references to __future__;

These will be taken care of via separate commits (for ease of review).

Change-Id: Ib44bda6d5e12be44f73c7ea65d6f6e610efb872d
TN: U530-006
2021-10-19 13:17:32 -07:00
Joel Brobecker
956950ee67 Remove all calls to "cd" in all testcases
All these calls are now superflous, so this commit removes them.

Note that this change was made entirely automatically, using
the following command:

    $ cd testsuite/tests
    $ perl -i -0pe 's/^\s*cd\([^\n]*\n+//gms' */run_test.py

Translation: Find all lines that start with spaces, followed by "cd(",
and then delete everything, including empty lines after it (if any).

The following command was then executed to automatically remove
all imports of "cd", which are now superfluous:

    $ cd testsuite/tests
    $ perl -i -e 's/ cd,//' */run_test.py

This helps our goal of avoiding changes to the global environment,
which is a requirement before we can transition the testsuite to
pytest.

TN: U530-006
Change-Id: Ia84162550648e622d8ab2d1b3210c8c4eeb0219b
2021-07-04 19:13:21 -07:00
Joel Brobecker
9c970a4716 format testsuite/tests/* with black, include them in pre-commit checks
While many testcases don't meet flake8 requirements right now
(the most prevalent one is the "*" import, but we also have
expected output strings with lines longer than 88 characters),
we can still improve the testcases using black to format them.
If anything, this will avoid to break many of flake8's rules.

This commit changes the .pre-commit-config.yaml file to remove
the exclusion for testsuite/tests/*. From there, all the files
in testsuite/tests/ were reformatted via:

    $ pre-commit run --all

Change-Id: Ia4957899cabf909f401bb2a242ed7035d89f1df9
2021-06-27 10:07:36 -07:00
Joel Brobecker
e50b4a23bc Introduce a TestCase.run "method" (for transition to pytest)
This commit introduces a new run attribute in our testsuite's
TestCase class as a way to allow testcases to call this method
instead of instantiating the Run class. All testcases are then
modified to use this method, by running the following perl
command:

    $ cd testsuite/tests
    $ perl -pi -e 's/\bRun\(/testcase.run(/g' **/run_test.py

The testcases are then modified to remove the unused import using
the following perl command:

    $ perl -pi -e 's/\s+Run,//g' **/run_test.py

The goal is to help the transition to pytest, where the testcases
will be using the "run" method to call external commands such as
Git. The difference is that the pytest version of this method
will do more than being a simple wrapper (e.g. it will take care
of the default working directory).

TN: U530-006
Change-Id: I1823ef4331d8d16da6cf5f4a4efdbb667c68848d
2021-06-27 10:07:36 -07:00
Joel Brobecker
2132e6fb4a Rename "self" parameter to "testcase" in all our testcases
This is another preparation patch towards the transition of
the testsuite framework to pytest (as part of the transition
to Python 3.x), where we will use a fixture called "testcase"
to replace the TestCase object (self).

To facilitate the transition as well as the review of that (future)
transition, this commit renames the "self" parameter into "testcase".
Note that the change was performed entirely automatically thanks to
the following perl command:

    perl -pi -e 's/\bself\b/testcase/g' **/run_test.py

It's a departure from the convention that the object for which
a method is called be called "self", but it's only a temporary
situation; once we transition the testsuite to pytest, we will
no longer have classes -- just simple functions.

TN: U530-006
Change-Id: Id9cf32c8a8f1ed0c2fd4236f303c8fbd7b4894c1
2021-06-27 10:07:36 -07:00
Joel Brobecker
eaef13cfb5 simplify "diff" section computation in commit emails
This commit is inspired by the fact that I couldn't understand
why I was skipping the first character from the output of a Git
command whe computing a commit's "diff", like so):

    diff = git.show(commit.rev, [...], pretty="format:|")[1:]

(emphasis on the "[1:]" at the end).

To understand, I remove the subscripting and reran the testsuite
without it to see what failures I would get. This gave me
the answer, which is we were intentionally starting the "format:"
string with a "|", and so we needed to strip that extra character.

That's when I found a comment I wrote; I didn't see it at first
because it was placed further up:

     # For the diff, there is one subtlelty:
     # Git commands calls strip on the output, which is usually
     # a good thing, but not in the case of the diff output.
     # Prevent this from happening by putting an artificial
     # character at the start of the format string, and then
     # by stripping it from the output.

This may have made sense back when I wrote that comment, but
we no longer strip the start of the output anymore (see commit
af06d5ea54). So I decided to
simplify the code by removing the extraneous character in
the "format:" string.

As it happens, this revealed that git behaves slightly differently
when given an empty "format:" string. Before:

    | $ git show -p -M --stat --pretty="format:|" HEAD
    | |---
    |  hooks/git.py | 4 +---
    |  1 file changed, 1 insertion(+), 3 deletions(-)
    |
    | diff --git a/hooks/git.py b/hooks/git.py
    | index fe2b36b..0669111 100644
    | [snip]

After (removing the "|" in the "format:" string):

    | $ git show -p -M --stat --pretty="format:" HEAD
    |  hooks/git.py | 4 +---
    |  1 file changed, 1 insertion(+), 3 deletions(-)
    |
    | diff --git a/hooks/git.py b/hooks/git.py
    | [snip]

What we can see is that the "---" separate line is no longer shown
in the second command.

Rather than forcing Git to print it, or rather than staying with
the existing code, this commit simply hardcodes the separator line.
One minor bonus of doing it this way is that, if Git decides to
change that separator, this won't affect us, and thus we won't
have to change hundreds of tests accordingly.

And by doing so, this revealed that there was actually an inconsistency
in the formatting produced by Git: In some cases (e.g. merge commits),
it became apparent that Git was omitting this "---" separator line,
even when the "format:" string was empty. The corresponding testcases
where the inconsistency showed up were adjusted to match the new
behavior, which is consisdered (slightly) better, because more
consistent.

Found while working on U530-006 (transition to Python 3.x).

Change-Id: Ifc473fa471ba618e11c3c4bcc6d83cc6f82fc6bf
2021-06-05 17:32:18 -07:00
Joel Brobecker
fe24b59970 always send all emails using a UTF-8 charset
This commit simplifies the choice of the charset being used to send
our emails to just using UTF-8. This makes all emails consistently using
that charset, and in particular follows something we were already doing
when the message body was found to be in unicode format (this happens
when the message body comes from calling one of the project's hooks).

The expectation is that this preliminary change will facilitate
the transition to Python 3, where strings are unicode.

Change-Id: I0e44baf460dd99a2505d94671ac6042304addfd2
TN: TB22-002
2021-05-03 05:14:07 -07:00
Joel Brobecker
54877f91a3 testsuite: change the testcase filename from "test.py" to "run_test.py"
This is a small step towards migrating this testsuite towards
a pytest-based framework: The filename currently used for testcases
is "test.py", and that filename does not match the nomenclature
that pytest expects for testcase files. So this commit renames
all our testcases to something that pytest can identify as a testcase
file to load and execute, and then adjusts the current testsuite
framework accordingly.

Change-Id: I77fba65e8ea20612259c6ad10459fe96ef77a2fc
2021-04-18 18:57:38 +04:00
Simon Marchi
a2909b2c8d Convert print statements to print function
In preparation for the transition to Python 3, convert to using the
print function.  The special import

    from __future__ import print_function

in Python 2.7 makes "print" become the function and not the statement.
In Python 3, it has no effect.  Once the transition is done and we
exclusively use Python 3, we can simply remove the imports.

The testsuite shows no regressions.
2020-11-22 19:29:38 -05:00
Joel Brobecker
86c6f4f314 fix testcase's hooks config to use consistent (space-based) indentation
All the changes were performed automatically by a script, which
did the following actions:
  - fixup the indentation in the refs/meta/config:project.config file
  - for all test repositories that had a local branch called
    'meta/config', rebase that branch on top of the change we just
    made.

This affects the following testcases, which are trying to push changes
to the refs/meta/config reference, since the changes we made cause
various SHA1s to change as a result:
  - meta_config_update
  - meta_config_update_with_other_ref/

Change-Id: I035732c6ebb4996c644e49fc604c02c5f67eeaeb
TN: T704-001
2020-07-08 15:16:22 -07:00
Joel Brobecker
4219203e35 update nearly all testcases to add hooks.filer-email to test repo config
This updates all the testcases where the hooks.filer-email is implicit,
so as to now be explicitly part of the test repository's configuration.

While we're making this change, we take this opportunity to use
a bogus email address instead of using an email address in one of
AdaCore's domains.

In most testcases, the test repositories' configuration was adjusted
using the following shell script:

    | #/usr/bin/env zsh
    |
    | test_dir=$1
    |
    | if [ ! -d $test_dir ]; then
    |    exit 1
    | fi
    |
    | cd $test_dir
    | if [ ! -f hooks_config ]; then
    |    exit 1
    | fi
    |
    | ../../bin/unpack-test-repos
    | (
    |   cd repo
    |   current_branch=`git rev-parse --abbrev-ref HEAD`
    |
    |   git fetch origin refs/meta/config
    |   git co FETCH_HEAD
    |   git config -f project.config hooks.filer-email 'filer@example.com'
    |   git ci -a -m 'project.config: add hooks.filer-email'
    |   git push origin HEAD:refs/meta/config
    |
    |   git co $current_branch
    | ) >/dev/null 2>&1
    | ../../bin/pack-test-repos

And the test.py scripts have then been adjusted with the following perl
one-liner:

    $ perl -pi -e "s/file-ci\@gnat.com/filer\@example.com/g" */test.py

A number of testscases (13, to be exact), had to be adjusted manually,
for a couple of reasons:

  - The test repository had a branch that the test tries to push to
    the refs/meta/config reference -- that push was being rejected
    because it was no longer up to date (non-fast-forward update).
    The branch was rebased, which changed the SHA1-s, and the diffs.

  - The test was creating a commit on the fly that changes the
    project.config file in the refs/meta/config reference. For those,
    the change to make was to simply update the hooks_config file
    in that test.

Change-Id: I626a29f77c3c2bb5dc13ebd2f11542a6fa066930
TN: T512-052
2020-06-15 11:32:45 -07:00
Joel Brobecker
3fed778872 rename check_commit and check_files to style_check_{commit,files}
This makes it clear what this functions are meant to do.
2017-08-04 16:06:47 -07:00
Joel Brobecker
e4fa3fd38b pre_commit_checks: pass the filename via stdin rather than the command line
This is preparation work towards calling the style checker only once
for an entire commit, rather than once per file.

This commit also adds a new testcase, allowing the code coverage to
remain at 100%.

For Q530-009.
2017-07-05 09:10:59 -07:00
Joel Brobecker
8333edae90 simplify module name in call to style_checker
Thanks to the transition to the new style checker, we can now
call it directly with the module name, without having to fake
the SVN path to that module.

Part of NA17-007.
2017-05-18 09:34:28 -07:00
Joel Brobecker
0ac0eeab07 Add a X-Git-Author: field in all emails sent.
For N813-004.
2014-08-14 08:34:57 -07:00
Joel Brobecker
ad437bf477 Create project.config in refs/meta/config branch (from hooks_config).
We are still preserving the hooks_config file within the test
directory in order to help grep-ability, but this is no longer
the master.  The pack-test-repos script has been updated to grab
and update our convenience copy to match the latest master.

Similarly, the unpack-test-repos script has been updated to use
the project.config file (ie, use the master copy) to populate
the bare repository's config file. It is expected that this part
will become unnecessary at some point, when the hooks are enhanced
to get their config from this file, instead of the repository's
config file.

For MC20-031.
2013-12-27 15:44:02 +04:00
Joel Brobecker
0c02d4bb4a Split the bare repository's config into two: git config vs hooks config.
This is a first step towards moving the hook's configuration
inside the repository itself.

For MC20-031.
2013-12-27 13:28:00 +04:00
Joel Brobecker
667afc5b4e Update expected output in testsuite to 1.8.1.2. 2013-10-10 15:29:43 +04:00
Joel Brobecker
dc3b28392b wrong commit use as reference for commit-per-commit pre-commit-check.
This patch should fix problems occurring when pushing merge commits.
See large comment in updates.__added_commits for more info.

Update all relevant testcases. And add a couple of testcases, one
in the usual mode, and one in combined-style-checking mode. Note
that the addition of the new testcases was not strictly necessary,
since several older testcase had clues showing a related issue
as well. For instance, merge_commit_branch_update shows that we had
a testcase that showed we were style-checking the wrong file.
But the new testcases are nice to have anyway, as it also allows us
to test more specifically for the situation that triggered this change.

For M606-034.
2013-06-07 14:24:57 +04:00