Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply: add unit tests for parse_range #1677

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

philip-peterson
Copy link

@philip-peterson philip-peterson commented Feb 19, 2024

Add unit tests for apply's parse_range function. Also rename parse_range to parse_fragment_range and expose it externally for use by the unit tests. Internal callers may continue to refer to it by the name parse_range.

It is necessary to make the function be non-internal linkage in order to expose it to the unit testing framework. Because there is another function in the codebase also called parse_range, change this one to a more specific name as well: parse_fragment_range. Also add several test cases (both positive and negative) for the function.

cc: "Kristoffer Haugsbakk" [email protected]
cc: Philip [email protected]

Copy link

There are issues in commit 9f88d9c:
apply: add unit tests for parse_range and rename parse_range to parse_fragment_range
First line of commit message is too long (> 76 columns)
Lines in the body of the commit messages should be wrapped between 60 and 76 characters.
Indented lines, and lines without whitespace, are exempt

Copy link

There are issues in commit 62c536e:
apply: rewrite unit tests with structured cases
Lines in the body of the commit messages should be wrapped between 60 and 76 characters.
Indented lines, and lines without whitespace, are exempt

Copy link

There are issues in commit 9f88d9c:
apply: add unit tests for parse_range and rename parse_range to parse_fragment_range
First line of commit message is too long (> 76 columns)
Lines in the body of the commit messages should be wrapped between 60 and 76 characters.
Indented lines, and lines without whitespace, are exempt

Copy link

There are issues in commit 99863bb:
apply: rewrite unit tests with structured cases
Lines in the body of the commit messages should be wrapped between 60 and 76 characters.
Indented lines, and lines without whitespace, are exempt

Copy link

There are issues in commit 597cffd:
apply: add unit tests for parse_range and rename parse_range to parse_fragment_range
First line of commit message is too long (> 76 columns)

@philip-peterson philip-peterson force-pushed the peterson/unit-tests branch 2 times, most recently from 6634c85 to a865f07 Compare February 19, 2024 03:08
@philip-peterson
Copy link
Author

/preview

Copy link

Preview email sent as [email protected]

@philip-peterson
Copy link
Author

/preview

Copy link

Preview email sent as [email protected]

@philip-peterson
Copy link
Author

/submit

Copy link

Submitted as [email protected]

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-git-1677/philip-peterson/peterson/unit-tests-v1

To fetch this version to local tag pr-git-1677/philip-peterson/peterson/unit-tests-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-git-1677/philip-peterson/peterson/unit-tests-v1

@@ -1339,6 +1339,7 @@ THIRD_PARTY_SOURCES += compat/regex/%
THIRD_PARTY_SOURCES += sha1collisiondetection/%

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the Git mailing list, Junio C Hamano wrote (reply to this):

"Philip Peterson via GitGitGadget" <[email protected]> writes:

> From: Philip Peterson <[email protected]>
>
> This patchset makes the parse_range function in apply be non-internal
> linkage in order to expose to the unit testing framework. In so doing,
> because there is another function called parse_range, I gave this one a more
> specific name, parse_fragment_range. Other than that, this commit adds
> several test cases (positive and negative) for the function.

We do not write "I did this, I did that" in our proposed log
message.  In addition, guidance on the proposed commit log in a
handful of sections in Documentation/SubmittingPatches would be
helpful.

It may probably be a good idea to split this into a preliminary
patch that makes a symbol extern (and doing nothing else), and
the main patch that adds external caller(s) to the function from
a new unit test.

It certainly is better than doing nothing and just make it extern,
but I am not sure "fragment" is specific enough to make the symbol
clearly belong to "apply" API.

> diff --git a/apply.c b/apply.c
> index 7608e3301ca..199a1150df6 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -1430,8 +1430,8 @@ static int parse_num(const char *line, unsigned long *p)
>  	return ptr - line;
>  }
>  
> -static int parse_range(const char *line, int len, int offset, const char *expect,
> -		       unsigned long *p1, unsigned long *p2)
> +int parse_fragment_range(const char *line, int len, int offset, const char *expect,
> +			 unsigned long *p1, unsigned long *p2)
>  {
>  	int digits, ex;

Alternatively we could do something like this to make the blast
radius of this patch smaller.

    -static int parse_range(const char *line, int len, int offset, const char *expect,
    +#define apply_parse_fragment_range parse_range
    +int parse_range(const char *line, int len, int offset, const char *expect,
                           unsigned long *p1, unsigned long *p2)

If not for unit-test, this function has no reason to be extern with
such a long name, so it is better to allow internal callers to refer
to it with the name that has been good enough for them for the past
19 years since it was introduced in fab2c257 (git-apply: make the
diffstat output happen for "--stat" only., 2005-05-26).

> diff --git a/apply.h b/apply.h
> index 7cd38b1443c..bbc5e3caeb5 100644
> --- a/apply.h
> +++ b/apply.h
> @@ -187,3 +187,7 @@ int apply_all_patches(struct apply_state *state,
>  		      int options);
>  
>  #endif
> +
> +
> +int parse_fragment_range(const char *line, int len, int offset, const char *expect,
> +		       unsigned long *p1, unsigned long *p2);

This is wrong.  The #endif is about avoiding double inclusion of
this header file and any new declaration must go before it.

> diff --git a/t/unit-tests/t-apply.c b/t/unit-tests/t-apply.c
> new file mode 100644

This should go to the next patch.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the Git mailing list, Philip wrote (reply to this):

Hi,

Thanks for the tips. I am confused about the change request though:

> Alternatively we could do something like this to make the blast
> radius of this patch smaller.
>
> -static int parse_range(const char *line, int len, int offset, const char *expect,
> +#define apply_parse_fragment_range parse_range
> +int parse_range(const char *line, int len, int offset, const char *expect,
>                         unsigned long *p1, unsigned long *p2)

From what I understand, this still creates a new extern symbol
called parse_range. The hope was to avoid creating a new symbol
with a generic name that someone might start to consume, because
if they did start consuming that symbol, it would be a burden on
them when we eventually have to rename it due to the conflict with
the other symbol currently called parse_range in add-patch.c, which
we might also want to add unit tests for later.  Is it not
preferable to just rename it now, before it becomes extern?

Cheers,
Phil

On Mon, Feb 19, 2024 at 4:35 PM Junio C Hamano <[email protected]> wrote:
>
> "Philip Peterson via GitGitGadget" <[email protected]> writes:
>
> > From: Philip Peterson <[email protected]>
> >
> > This patchset makes the parse_range function in apply be non-internal
> > linkage in order to expose to the unit testing framework. In so doing,
> > because there is another function called parse_range, I gave this one a more
> > specific name, parse_fragment_range. Other than that, this commit adds
> > several test cases (positive and negative) for the function.
>
> We do not write "I did this, I did that" in our proposed log
> message.  In addition, guidance on the proposed commit log in a
> handful of sections in Documentation/SubmittingPatches would be
> helpful.
>
> It may probably be a good idea to split this into a preliminary
> patch that makes a symbol extern (and doing nothing else), and
> the main patch that adds external caller(s) to the function from
> a new unit test.
>
> It certainly is better than doing nothing and just make it extern,
> but I am not sure "fragment" is specific enough to make the symbol
> clearly belong to "apply" API.
>
> > diff --git a/apply.c b/apply.c
> > index 7608e3301ca..199a1150df6 100644
> > --- a/apply.c
> > +++ b/apply.c
> > @@ -1430,8 +1430,8 @@ static int parse_num(const char *line, unsigned long *p)
> >       return ptr - line;
> >  }
> >
> > -static int parse_range(const char *line, int len, int offset, const char *expect,
> > -                    unsigned long *p1, unsigned long *p2)
> > +int parse_fragment_range(const char *line, int len, int offset, const char *expect,
> > +                      unsigned long *p1, unsigned long *p2)
> >  {
> >       int digits, ex;
>
> Alternatively we could do something like this to make the blast
> radius of this patch smaller.
>
>     -static int parse_range(const char *line, int len, int offset, const char *expect,
>     +#define apply_parse_fragment_range parse_range
>     +int parse_range(const char *line, int len, int offset, const char *expect,
>                            unsigned long *p1, unsigned long *p2)
>
> If not for unit-test, this function has no reason to be extern with
> such a long name, so it is better to allow internal callers to refer
> to it with the name that has been good enough for them for the past
> 19 years since it was introduced in fab2c257 (git-apply: make the
> diffstat output happen for "--stat" only., 2005-05-26).
>
> > diff --git a/apply.h b/apply.h
> > index 7cd38b1443c..bbc5e3caeb5 100644
> > --- a/apply.h
> > +++ b/apply.h
> > @@ -187,3 +187,7 @@ int apply_all_patches(struct apply_state *state,
> >                     int options);
> >
> >  #endif
> > +
> > +
> > +int parse_fragment_range(const char *line, int len, int offset, const char *expect,
> > +                    unsigned long *p1, unsigned long *p2);
>
> This is wrong.  The #endif is about avoiding double inclusion of
> this header file and any new declaration must go before it.
>
> > diff --git a/t/unit-tests/t-apply.c b/t/unit-tests/t-apply.c
> > new file mode 100644
>
> This should go to the next patch.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the Git mailing list, Junio C Hamano wrote (reply to this):

Philip <[email protected]> writes:

> On Mon, Feb 19, 2024 at 4:35 PM Junio C Hamano <[email protected]> wrote:
>>
>> "Philip Peterson via GitGitGadget" <[email protected]> writes:

It's quite a blast from a long time ago that I no longer remember.

>> Alternatively we could do something like this to make the blast
>> radius of this patch smaller.
>>
>> -static int parse_range(const char *line, int len, int offset, const char *expect,
>> +#define apply_parse_fragment_range parse_range
>> +int parse_range(const char *line, int len, int offset, const char *expect,
>>                         unsigned long *p1, unsigned long *p2)
>
> From what I understand, this still creates a new extern symbol
> called parse_range.

Sorry, I misspoke.  The direction of #define is the other way
around.  That is, we may have to give the function a name that is
overly long because it needs to be externally linkable only to
support for your test, but we would want to locally rename that long
name down to the name currently used by the primary callers of that
function, so that the patch does not have to touch these existing
calling sites.  After all, this function is a small implementation
detail and not a part of the official apply.c API, and the only
reason why we are making it extern is because some new tests want to
link with it from the side.

So, in the <apply.h> header file you'll do

	/* 
	 * exposed only for tests; do not call this as it not
	 * a part of the API
	 */
	extern int apply_parse_fragment_range(...);

and then in the original file that used to have "static int
parse_range(...)", you'd add

	#define parse_range apply_parse_fragment_range

near the top, after the header files are included.

Thanks.

@@ -0,0 +1,100 @@
#include "test-lib.h"
#include "apply.h"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the Git mailing list, Junio C Hamano wrote (reply to this):

"Philip Peterson via GitGitGadget" <[email protected]> writes:

> From: Philip Peterson <[email protected]>
>
> The imperative format was a little hard to read, so I rewrote the test cases
> in a declarative style by defining a common structure for each test case and
> its assertions.
>
> Signed-off-by: Philip Peterson <[email protected]>
> ---
>  t/unit-tests/t-apply.c | 123 ++++++++++++++++++++++++++---------------
>  1 file changed, 78 insertions(+), 45 deletions(-)

In this project, we do not add a version of a known-to-be-bad file
in patch [1/2], only to be immediately improved in patch [2/2].
Unless, of course, there is a good reason to do so.  And "to
preserve the true history of what happened in the developer's
working tree" is not a good reason.

We give our developers "rebase -i" and other means to rewrite their
Git history, not just because we want them to be able to pretend
that they are a better developer who never make such a mistake or
misdesign in the first place, but because a polished history is
easier to review in the shorter term and to maintain in the longer
term.

Thanks.

@@ -0,0 +1,100 @@
#include "test-lib.h"
#include "apply.h"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the Git mailing list, "Kristoffer Haugsbakk" wrote (reply to this):

On Mon, Feb 19, 2024, at 05:45, Philip Peterson via GitGitGadget wrote:
> From: Philip Peterson <[email protected]>
>
> The imperative format was a little hard to read, so I rewrote the test cases
> in a declarative style by defining a common structure for each test case and
> its assertions.
>
> Signed-off-by: Philip Peterson <[email protected]>

IMO in general you can just assert that X and Y in the commit message.

  “ The imperative format is hard to read. Rewrite the test cases …

If your patch passes review and is merged then that’s the truth as
determined by you and the reviewers.

More subjective-sounding “This was hard to read” and maybe anecdotes
like “this tripped me up when reading” can go outside the commit message
like the cover letter or the free-form space between the commit message
and the patch (after the three-hyphen/three-dash lines).

-- 
Kristoffer Haugsbakk

Copy link

User "Kristoffer Haugsbakk" <[email protected]> has been added to the cc: list.

Copy link

User Philip <[email protected]> has been added to the cc: list.

Copy link

There are issues in commit c476258:
one
Commit checks stopped - the message is too short
Commit not signed off

Copy link

There are issues in commit 92f845b:
add unit test
Commit checks stopped - the message is too short
Commit not signed off

Copy link

There are issues in commit 8f4886c:
Requested changes
Commit checks stopped - the message is too short
Commit not signed off

Copy link

There are issues in commit cc744ed:
apply: add unit tests for parse_range
Commit not signed off

@philip-peterson philip-peterson force-pushed the peterson/unit-tests branch 2 times, most recently from b04f163 to d47dd76 Compare May 17, 2024 01:38
Also rename parse_range to parse_fragment_range for external linkage.

Signed-off-by: Philip Peterson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant