Skip to content

Commit

Permalink
FIX #361: utf8 file with BOM
Browse files Browse the repository at this point in the history
FIX #288: Use print function instead print statement in environment/steps files
  • Loading branch information
jenisys committed Jan 6, 2016
1 parent e0a0a67 commit 3541025
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 129 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ FIXED:
* issue #384: Active Tags fail with ScenarioOutline (submitted by: BRevzin)
* issue #383: Handle (custom) Type parsing errors better (submitted by: zsoldosp)
* pull #382: fix typo in tag name (provided by: zsoldosp)
* issue #361: utf8 file with BOM (provided by: karulis)
* issue #349: ScenarioOutline skipped with --format=json
* issue #319: python-version requirements in behave.whl for Python2.6 (submitted by: darkfoxprime)
* issue #310: Use setuptools_behave.py with behave module
* issue #309: behave --lang-list fails on Python3 (and Python2)
* issue #288: Use print function instead print statement in environment/steps files


Version: 1.2.5 (2015-01-31)
Expand Down
10 changes: 2 additions & 8 deletions behave/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,11 @@ def exec_file(filename, globals_=None, locals_=None):
if locals_ is None:
locals_ = globals_
locals_["__file__"] = filename
with open(filename) as f:
with open(filename, "rb") as f:
# pylint: disable=exec-used
# -- FIX issue #80: exec(f.read(), globals_, locals_)
# try:
filename2 = os.path.relpath(filename, os.getcwd())
code = compile(f.read(), filename2, "exec")
code = compile(f.read(), filename2, "exec", dont_inherit=True)
exec(code, globals_, locals_)
# except Exception as e:
# e_text = _text(e)
# print("Exception %s: %s" % (e.__class__.__name__, e_text))
# raise


def path_getrootdir(path):
Expand Down
19 changes: 13 additions & 6 deletions behave4cmd0/command_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,29 @@ def step_use_curdir_as_working_directory(context):
# -----------------------------------------------------------------------------
# STEPS: Create files with contents
# -----------------------------------------------------------------------------
@given(u'a file named "{filename}" with')
def step_a_file_named_filename_with(context, filename):
"""
Creates a textual file with the content provided as docstring.
"""
@given(u'a file named "{filename}" and encoding="{encoding}" with')
def step_a_file_named_filename_and_encoding_with(context, filename, encoding):
"""Creates a textual file with the content provided as docstring."""
__encoding_is_valid = True
assert context.text is not None, "ENSURE: multiline text is provided."
assert not os.path.isabs(filename)
assert __encoding_is_valid
command_util.ensure_workdir_exists(context)
filename2 = os.path.join(context.workdir, filename)
pathutil.create_textfile_with_contents(filename2, context.text)
pathutil.create_textfile_with_contents(filename2, context.text, encoding)


@given(u'a file named "{filename}" with')
def step_a_file_named_filename_with(context, filename):
"""Creates a textual file with the content provided as docstring."""
step_a_file_named_filename_and_encoding_with(context, filename, "UTF-8")

# -- SPECIAL CASE: For usage with behave steps.
if filename.endswith(".feature"):
command_util.ensure_context_attribute_exists(context, "features", [])
context.features.append(filename)


@given(u'an empty file named "{filename}"')
def step_an_empty_file_named_filename(context, filename):
"""
Expand Down
202 changes: 88 additions & 114 deletions issue.features/issue0288.feature
Original file line number Diff line number Diff line change
@@ -1,121 +1,95 @@
@issue
@only.with_python2=true
Feature: Issue #288 -- print() needed in "environment.py" file or steps

. QUICKFIX: Apply to your code base
. pip install modernize
. python-modernize --fix=libmodernize.fixes.fix_print my_directory/ # Shows only changes
. python-modernize --fix=libmodernize.fixes.fix_print --write my_directory/ # Applies changes
.
. NOTE: Currently, the feature describes the implemented behaviour of behave.

Background: Feature Setup
Given a new working directory
And a file named "features/steps/steps.py" with:
"""
from __future__ import unicode_literals
from behave import step
@step('{word:w} step passes')
def step_passes(context, word):
pass
"""
And a file named "features/e1.feature" with:
"""
Feature:
Scenario: Alice
Given a step passes
Then another step passes
Scenario: Bob
Given some step passes
"""
Feature: Issue #288 -- Use print function instead print statement in environment/steps files

. Loaded files have future flags of importing module "behave.runner".
. This should be removed.
.
. AFFECTED FILES:
. * features/environment.py
. * features/steps/*.py
.
. AFFECTED FUTURE FLAGS:
. * print_function
. * absolute_import
. * ...

Scenario: Environment File Example
Given a file named "features/environment.py" with:
"""
def before_all(context):
print "HELLO @before_all"
"""
When I run "behave -f plain features"
Then it should fail with:
"""
File "features/environment.py", line 2
print "HELLO @before_all"
^
SyntaxError: invalid syntax
"""

Scenario: Module imported from environment file is unaffected
Given a file named "features/environment.py" with:
"""
from __future__ import absolute_import, print_function
from foo import hello
def before_all(context):
hello("@before_all")
"""
And a file named "foo.py" with:
"""
def hello(text):
print "HELLO "+ text
"""
When I run "behave -f plain features"
Then it should pass with:
"""
2 scenarios passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined
"""
@setup
Scenario: Feature Setup
Given a new working directory
And a file named "features/steps/reuse_steps.py" with:
"""
from behave4cmd0 import passing_steps
"""
And a file named "features/passing.feature" with:
"""
Feature:
Scenario:
Given a step passes
"""

Scenario: Steps Example
Given a file named "features/steps/print_steps.py" with:
"""
from __future__ import unicode_literals
from behave import step
@preferred
Scenario: Use print function with future-statement in steps/environment (PY2, PY3)
Given a file named "features/steps/my_steps.py" with:
"""
from __future__ import print_function
print("Hello step")
"""
And a file named "features/environment.py" with:
"""
from __future__ import print_function
print("Hello environment")
"""
When I run "behave -f plain features/passing.feature"
Then it should pass with:
"""
1 scenario passed, 0 failed, 0 skipped
1 step passed, 0 failed, 0 skipped, 0 undefined
"""
And the command output should not contain:
"""
Traceback (most recent call last):
"""

@step('I print "{text}"')
def step_print(context, text):
print "HELLO STEP "+ text
"""
When I run "behave -f plain features"
Then it should fail with:
"""
File "features/steps/print_steps.py", line 6
print "HELLO STEP "+ text
^
SyntaxError: invalid syntax
"""
@use.with_python2=true
Scenario: Use python2 print keyword in steps/environment
Given a file named "features/steps/my_steps.py" with:
"""
print "Hello step"
"""
And a file named "features/environment.py" with:
"""
print "Hello step"
"""
When I run "behave -f plain features/passing.feature"
Then it should pass with:
"""
1 scenario passed, 0 failed, 0 skipped
1 step passed, 0 failed, 0 skipped, 0 undefined
"""
And the command output should not contain "SyntaxError"
And the command output should not contain:
"""
Traceback (most recent call last):
"""

Scenario: Step imports other module with print statement
Given a file named "features/steps/print_steps2.py" with:
"""
from __future__ import unicode_literals
from behave import step
from bar import hello
@use.with_python3=true
Scenario: Use print function without future-statement in steps/environment (PY3)
Given a file named "features/steps/my_steps.py" with:
"""
print("Hello step")
"""
And a file named "features/environment.py" with:
"""
print("Hello environment")
"""
When I run "behave -f plain features/passing.feature"
Then it should pass with:
"""
1 scenario passed, 0 failed, 0 skipped
1 step passed, 0 failed, 0 skipped, 0 undefined
"""
And the command output should not contain:
"""
Traceback (most recent call last):
"""

@step('I print "{text}"')
def step_print(context, text):
hello(text)
"""
And a file named "bar.py" with:
"""
def hello(text):
print "HELLO "+ text
"""
And a file named "features/e2.feature" with:
"""
Feature:
Scenario: Charly
Given I print "Lorem ipsum"
"""
When I run "behave -f plain --no-capture features/e2.feature"
Then it should pass with:
"""
1 scenario passed, 0 failed, 0 skipped
1 step passed, 0 failed, 0 skipped, 0 undefined
"""
And the command output should contain:
"""
HELLO Lorem ipsum
"""
68 changes: 68 additions & 0 deletions issue.features/issue0361.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
@issue
Feature: Issue #361 -- UTF-8 File with BOM

. Using step files with "Byte-Order Mark" (BOM) prefix
. causes weird problems.
.
. AFFECTED FILES:
. * features/environment.py
. * features/steps/*.py

@setup
Scenario: Feature Setup
Given a new working directory
And a file named "features/steps/reuse_steps.py" with:
"""
from behave4cmd0 import passing_steps
"""
And a file named "features/passing.feature" with:
"""
Feature:
Scenario:
Given a step passes
Then a special step
"""

@encoding.<encoding>
Scenario Outline: Use step file with <case>
Given a file named "features/steps/my_steps.py" and encoding="<encoding>" with:
"""
# -*- coding: <encoding> -*-
'''
<text>.
'''
from behave import step
@step(u'a special step')
def step_impl(context):
pass
"""
When I run "behave -f plain features/passing.feature"
Then it should pass with:
"""
1 scenario passed, 0 failed, 0 skipped
2 steps passed, 0 failed, 0 skipped, 0 undefined
"""
And the command output should contain:
"""
Feature:
Scenario:
Given a step passes ... passed
Then a special step ... passed
"""
And the command output should not contain:
"""
SyntaxError: invalid character in identifier
"""

Examples:
| encoding | text | case | Comment |
| UTF-8-sig | Ärgernis ist überall | UTF-8 encoding and BOM | |
| latin1 | Café | Latin1 encoding | |
| iso-8859-1 | Ärgernis ist überall | iso-8859-1 encoding | Alias for latin1 |
| cp1252 | Ärgernis ist überall | cp1252 encoding | Windows: Western Europe |
| cp1251 | Привет! (=hello) | cp1251 encoding (Russia)| Windows: Russia |
| cp866 | Привет! (=hello) | cp688 encoding (Russia) | IBM866: Russia |
| euc_jp | こんにちは (=hello) | euc_jp encoding (Japan) | Japanese |
| gbk | 您好 (=hello) | gbk encoding (China) | Unified Chinese |
| gb2312 | 您好 (=hello) | gb2312 encoding (China) | Simplified Chinese |
3 changes: 2 additions & 1 deletion issue.features/issue0383.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Feature: Issue #383 -- Handle (custom) Type parsing errors better


Scenario: Type conversion fails
Given a file named "features/steps/bad_type_converter_steps.py" with:
Given a new working directory
And a file named "features/steps/bad_type_converter_steps.py" with:
"""
from behave import step, register_type
import parse
Expand Down

0 comments on commit 3541025

Please sign in to comment.