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

Missing trailing : on emitted with statement #18

Open
jayvdb opened this issue Jun 26, 2017 · 7 comments
Open

Missing trailing : on emitted with statement #18

jayvdb opened this issue Jun 26, 2017 · 7 comments

Comments

@jayvdb
Copy link

jayvdb commented Jun 26, 2017

The tool chokes on https://github.com/coala/coala/blob/8a25983/tests/parsing/CliParsingTest.py#L58

Somehow it writes with statements without a trailing :.

         sections = parse_cli(arg_list=['--relpath'])
-        with self.assertRaisesRegex(SystemExit, '2') as cm:
+        with pytest.raises(SystemExit) as excinfo
+        assert re.search(pattern, excinfo.value) as cm:
             check_conflicts(sections)
-            self.assertEqual(cm.exception.code, 2)
+            assert cm.exception.code == 2
 
         sections = parse_cli(arg_list=['--output', 'iraiseValueError'])
-        with self.assertRaisesRegex(SystemExit, '2') as cm:
+        with pytest.raises(SystemExit) as excinfo
+        assert re.search(pattern, excinfo.value) as cm:
             check_conflicts(sections)
-            self.assertEqual(cm.exception.code, 2)
+            assert cm.exception.code == 2

The result is naturally a syntax error: https://travis-ci.org/jayvdb/coala/jobs/246949584#L9068

@jayvdb
Copy link
Author

jayvdb commented Jun 26, 2017

I suspect it is failing on with .. as ..:

@htgoebel
Copy link
Collaborator

Can you please provide a minimal example and use context-diff. I can't spot what is changed here. Thanks.

@htgoebel
Copy link
Collaborator

htgoebel commented Aug 16, 2017

The problem is that within

with self.assertRaisesRegex(SystemExit, '2') as cm:

the self.assertRaisesRegex(…) gets expanded to

pytest.raises(SystemExit) as excinfo:
        assert re.search(pattern, excinfo.value)

Easy As a quick solution we could do this when converting the code

  • Check if we are in a with-clause
  • If so, don't change the code, but issue a warning.

Help wanted: In the long-run:

  • Maybe it's easier to implement a new fix using a more specific pattern.
  • Check if we are in a with-clause
  • Get the as-target name, if there is one, defaulting to excinfo
  • Replace the self.assertRaisesRegex(…) only
  • Insert assert re.search(pattern, excinfo.value) as first statement in the with-block
  • If there was an as-target given, issue a warning that the code still needs to be adjusted, since in the target's attributes are not compatible between unittest and pytest.
    An enhance solution would be to try to determine if the target name is used within the with-block and issue the warning only then. Wow, and the even enhanced version would be to try to rewrite the attribute access. Volunteers are very welcome :-)

@nicoddemus
Copy link
Member

FWIW since 2.10 pytest.raises as context-manager supports a match keyword which matches with re.search, so:

with self.assertRaisesRegex(SystemExit, '2') as cm:

Can be rewritten directly as:

with pytest.raises(SystemExit, match='2') as cm:

@htgoebel
Copy link
Collaborator

Using the match keyword could be an elegant solution, but it would impose that any test-suite converted with unittest2pytest requires an up-to-date version of pytest.

I'm fine with this, since any project converting now ought not have "pinned" to a specific version of pytest. But we should be aware of this, decide about it and document.

For the records: Even if the docs say this was added in 2.10, the changelog says it was added in 3.1.0 (2017-05-22)

@nicoddemus
Copy link
Member

For the records: Even if the docs say this was added in 2.10, the changelog says it was added in 3.1.0 (2017-05-22)

Oops thanks for catching that, I will fix it in the docs. 👍

@nicoddemus
Copy link
Member

xref: pytest-dev/pytest#2697

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants