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

assert_error_msg_content_equals() regexp is too greedy #316

Closed
Gumix opened this issue Jul 26, 2023 · 1 comment · Fixed by #333
Closed

assert_error_msg_content_equals() regexp is too greedy #316

Gumix opened this issue Jul 26, 2023 · 1 comment · Fixed by #333
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Gumix
Copy link

Gumix commented Jul 26, 2023

if stripFileAndLine then
if error_msg:gsub("^.+:%d+: ", "") ~= expectedMsg then
differ = true
end
else

It doesn't match error_msg, which contains the following pattern:

local error_msg = "foo:1: bar"
t.assert_error_msg_content_equals(error_msg, error, error_msg)
[001] not ok 3  gh-0000.engine:"memtx".test_aaaa
[001] #   ...engine-luatest/gh_0000_test.lua:231: Error message expected: "foo:1: bar"
[001] #   Error message received: "foo:1: bar"
@ylobankov ylobankov added the bug Something isn't working label Sep 15, 2023
ochaplashkin pushed a commit to ochaplashkin/luatest that referenced this issue Oct 24, 2023
> What's the problem?

If the user makes a mistake in the `assert_error_msg_content_equals`:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")

then the following error will be returned:

    Error message expected: "bad"
    Error message received: "foo:1: bar"

The user thinks that a comparison is being made between `foo:1:1 bar`
and `bad`. He notices this error and at the same time can write the
following:

    t.assert_error_msg_content_equals("foo:1: bar", error, "foo:1: bar")

Now he will get the following error:

    Error message expected: "foo:1: bar"
    Error message received: "foo:1: bar"

It seems that the function doesn't work cause these strings are equals.
In fact, a comparison will be performed between `bar` and `foo:1: bar`.

The next use of the function will be correct:

    t.assert_error_msg_content_equals("bar", error, "foo:1: bar")

> What's the solution?

It is necessary to save the result of conversion after the `gsub`. So
this way the user will see the actual strings that are being compared:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")
    Error message expected: "bad"
    Error message received: "bar"

Close tarantool#316
ochaplashkin pushed a commit to ochaplashkin/luatest that referenced this issue Oct 24, 2023
> What's the problem?

If the user makes a mistake in the `assert_error_msg_content_equals`:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")

then the following error will be returned:

    Error message expected: "bad"
    Error message received: "foo:1: bar"

The user thinks that a comparison is being made between `foo:1:1 bar`
and `bad`. He notices this error and at the same time can write the
following:

    t.assert_error_msg_content_equals("foo:1: bar", error, "foo:1: bar")

Now he will get the following error:

    Error message expected: "foo:1: bar"
    Error message received: "foo:1: bar"

It seems that the function doesn't work cause these strings are equals.
In fact, a comparison will be performed between `bar` and `foo:1: bar`.

The next use of the function will be correct:

    t.assert_error_msg_content_equals("bar", error, "foo:1: bar")

> What's the solution?

It is necessary to save the result of conversion after the `gsub`. So
this way the user will see the actual strings that are being compared:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")
    Error message expected: "bad"
    Error message received: "bar"

Close tarantool#316
ochaplashkin pushed a commit to ochaplashkin/luatest that referenced this issue Oct 24, 2023
> What's the problem?

If the user makes a mistake in the `assert_error_msg_content_equals`:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")

then the following error will be returned:

    Error message expected: "bad"
    Error message received: "foo:1: bar"

The user thinks that a comparison is being made between `foo:1:1 bar`
and `bad`. He notices this error and at the same time can write the
following:

    t.assert_error_msg_content_equals("foo:1: bar", error, "foo:1: bar")

Now he will get the following error:

    Error message expected: "foo:1: bar"
    Error message received: "foo:1: bar"

It seems that the function doesn't work cause these strings are equals.
In fact, a comparison will be performed between `bar` and `foo:1: bar`.

The next use of the function will be correct:

    t.assert_error_msg_content_equals("bar", error, "foo:1: bar")

> What's the solution?

It is necessary to save the result of conversion after the `gsub`. So
this way the user will see the actual strings that are being compared:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")
    Error message expected: "bad"
    Error message received: "bar"

Close tarantool#316
ochaplashkin pushed a commit to ochaplashkin/luatest that referenced this issue Oct 25, 2023
> What's the problem?

If the user makes a mistake in the `assert_error_msg_content_equals`:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")

where:
    "bad" - excepted error message
    error - built-in function (there may be another function here)
    "foo:1: bar" - argument for the function

then the following error will be returned:

    Error message expected: "bad"
    Error message received: "foo:1: bar"

The user thinks that a comparison is being made between `foo:1:1 bar`
and `bad`. He notices this error and at the same time can write the
following:

    t.assert_error_msg_content_equals("foo:1: bar", error, "foo:1: bar")

Now he will get the following error:

    Error message expected: "foo:1: bar"
    Error message received: "foo:1: bar"

It seems that the function doesn't work cause these strings are equal.
In fact, a comparison will be performed between `foo:1: bar` (excepted)
and `bar` strings (because `foo:1: ` has been strip from error message
by regex).

The next use of the function will be correct:

    t.assert_error_msg_content_equals("bar", error, "foo:1: bar")

> What's the solution?

It is necessary to save the result of conversion after the `gsub`. So
this way the user will see the actual strings that are being compared:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")
    Error message expected: "bad"
    Error message received: "bar"

Close tarantool#316
ochaplashkin pushed a commit to ochaplashkin/luatest that referenced this issue Oct 25, 2023
> What's the problem?

If the user makes a mistake in the `assert_error_msg_content_equals`:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")

where:
    "bad" - excepted error message
    error - built-in function (there may be another function here)
    "foo:1: bar" - argument for the function

then the following error will be returned:

    Error message expected: "bad"
    Error message received: "foo:1: bar"

The user thinks that a comparison is being made between `foo:1: bar` and
`bad` strings. He notices this error and at the same time can write the
following:

    t.assert_error_msg_content_equals("foo:1: bar", error, "foo:1: bar")

Now he will get the following error:

    Error message expected: "foo:1: bar"
    Error message received: "foo:1: bar"

It seems that the function doesn't work cause these strings are equal.
In fact, a comparison will be performed between `foo:1: bar` (excepted)
and `bar` strings (because `foo:1: ` has been strip from error message
by regex).

The next use of the function will be correct:

    t.assert_error_msg_content_equals("bar", error, "foo:1: bar")

> What's the solution?

It is necessary to save the result of conversion after the `gsub`. So
this way the user will see the actual strings that are being compared:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")
    Error message expected: "bad"
    Error message received: "bar"

Close tarantool#316
ochaplashkin pushed a commit to ochaplashkin/luatest that referenced this issue Oct 25, 2023
> What's the problem?

If the user makes a mistake in the `assert_error_msg_content_equals`:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")

where:

    "bad" - excepted error message
    error - built-in function (there may be another function here)
    "foo:1: bar" - argument for the function

then the following error will be returned:

    Error message expected: "bad"
    Error message received: "foo:1: bar"

The user thinks that a comparison is being made between `foo:1: bar` and
`bad` strings. He notices this error and at the same time can write the
following:

    t.assert_error_msg_content_equals("foo:1: bar", error, "foo:1: bar")

Now he will get the following error:

    Error message expected: "foo:1: bar"
    Error message received: "foo:1: bar"

It seems that the function doesn't work cause these strings are equal.
In fact, a comparison will be performed between `foo:1: bar` (excepted)
and `bar` (actual, because `foo:1:` will be dropped from the error
message by regex).

The next use of the function will be correct:

    t.assert_error_msg_content_equals("bar", error, "foo:1: bar")

> What's the solution?

It is necessary to save the result of conversion after the `gsub`. So
this way the user will see the actual strings that are being compared:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")
    Error message expected: "bad"
    Error message received: "bar"

Close tarantool#316
ochaplashkin pushed a commit to ochaplashkin/luatest that referenced this issue Oct 25, 2023
> What's the problem?

If the user makes a mistake in the `assert_error_msg_content_equals`:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")

where:

    "bad" - expected error message
    error - built-in function (there may be another function here)
    "foo:1: bar" - argument for the function

then the following error will be returned:

    Error message expected: "bad"
    Error message received: "foo:1: bar"

The user thinks that a comparison is being made between `foo:1: bar` and
`bad` strings. He notices this error and at the same time can write the
following:

    t.assert_error_msg_content_equals("foo:1: bar", error, "foo:1: bar")

Now he will get the following error:

    Error message expected: "foo:1: bar"
    Error message received: "foo:1: bar"

It seems that the function doesn't work cause these strings are equal.
In fact, a comparison will be performed between `foo:1: bar` (expected)
and `bar` (actual, because `foo:1:` will be dropped from the error
message by regex).

The next use of the function will be correct:

    t.assert_error_msg_content_equals("bar", error, "foo:1: bar")

> What's the solution?

It is necessary to save the result of conversion after the `gsub`. So
this way the user will see the actual strings that are being compared:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")
    Error message expected: "bad"
    Error message received: "bar"

Close tarantool#316
ochaplashkin pushed a commit to ochaplashkin/luatest that referenced this issue Oct 25, 2023
> What's the problem?

If the user makes a mistake in the `assert_error_msg_content_equals`:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")

where:

    "bad" - expected error message
    error - built-in function (there may be another function here)
    "foo:1: bar" - argument for the function

then the following error will be returned:

    Error message expected: "bad"
    Error message received: "foo:1: bar"

The user thinks that a comparison is being made between `foo:1: bar` and
`bad` strings. He notices this error and at the same time can write the
following:

    t.assert_error_msg_content_equals("foo:1: bar", error, "foo:1: bar")

Now he will get the following error:

    Error message expected: "foo:1: bar"
    Error message received: "foo:1: bar"

It seems that the function doesn't work cause these strings are equal.
In fact, a comparison will be performed between `foo:1: bar` (expected)
and `bar` (actual, because `foo:1:` will be dropped from the error
message by regex).

The next use of the function will be correct:

    t.assert_error_msg_content_equals("bar", error, "foo:1: bar")

> What's the solution?

It is necessary to save the result of conversion after the `gsub`. So
this way the user will see the actual strings that are being compared:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")
    Error message expected: "bad"
    Error message received: "bar"

Close tarantool#316
ylobankov pushed a commit that referenced this issue Oct 25, 2023
> What's the problem?

If the user makes a mistake in the `assert_error_msg_content_equals`:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")

where:

    "bad" - expected error message
    error - built-in function (there may be another function here)
    "foo:1: bar" - argument for the function

then the following error will be returned:

    Error message expected: "bad"
    Error message received: "foo:1: bar"

The user thinks that a comparison is being made between `foo:1: bar` and
`bad` strings. He notices this error and at the same time can write the
following:

    t.assert_error_msg_content_equals("foo:1: bar", error, "foo:1: bar")

Now he will get the following error:

    Error message expected: "foo:1: bar"
    Error message received: "foo:1: bar"

It seems that the function doesn't work cause these strings are equal.
In fact, a comparison will be performed between `foo:1: bar` (expected)
and `bar` (actual, because `foo:1:` will be dropped from the error
message by regex).

The next use of the function will be correct:

    t.assert_error_msg_content_equals("bar", error, "foo:1: bar")

> What's the solution?

It is necessary to save the result of conversion after the `gsub`. So
this way the user will see the actual strings that are being compared:

    t.assert_error_msg_content_equals("bad", error, "foo:1: bar")
    Error message expected: "bad"
    Error message received: "bar"

Close #316
@Totktonada
Copy link
Member

AFAIU, assert_error_msg_equals() should be used in the case instead of assert_error_msg_content_equals().

@ylobankov ylobankov added this to the 1.0.0 milestone Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants