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

Allow different features dir #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions aloe/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
from aloe.exceptions import StepDiscoveryError


GHERKIN_FEATURES_DIR_NAME = 'GHERKIN_FEATURES_DIR_NAME'


def path_to_module_name(filename):
"""Convert a path to a file to a Python module name."""

Expand Down Expand Up @@ -65,16 +68,39 @@ def find_and_load_step_definitions(cls, dir_):
exc
)

@classmethod
def features_dirname(cls):
"""
Set the name of the features directory.
It is possible to change the name by setting the environment variable:
GHERKIN_FEATURES_DIR_NAME = 'gherkin_features'
"""

env = os.environ
features_dirname = env.get(GHERKIN_FEATURES_DIR_NAME, 'features')

# check the dir name is not a path
if '/' in features_dirname:
raise_from(
StepDiscoveryError("%s cannot be a path: %s" % (
GHERKIN_FEATURES_DIR_NAME, features_dirname)),
None
)

return features_dirname

@classmethod
def find_feature_directories(cls, dir_):
"""
Locate directories to load features from.

The directories must be named 'features'; they must either reside
The directories should be named 'features'; they must either reside
directly in the specified directory, or otherwise all their parents
must be packages (have __init__.py files).
"""

features_dirname = cls.features_dirname()

# A set of package directories discovered
packages = set()

Expand All @@ -85,8 +111,8 @@ def find_feature_directories(cls, dir_):

if path == dir_ or path in packages:
# Does this package have a feature directory?
if 'features' in dirs:
yield os.path.join(path, 'features')
if features_dirname in dirs:
yield os.path.join(path, features_dirname)

else:
# This is not a package, prune search
Expand Down
44 changes: 44 additions & 0 deletions tests/functional/test_step_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,47 @@ def test_all_features(self):
'submodule/features/third.feature',
)
])

def test_single_alternative_feature(self):
"""
Test running a single feature in the alternative features directory.
"""

os.environ['GHERKIN_FEATURES_DIR_NAME'] = 'alternative_features'

self.assert_feature_success('alternative_features/single.feature')

del os.environ['GHERKIN_FEATURES_DIR_NAME']

def test_alternative_subdirectory_feature(self):
"""
Test running a feature in a subdirectory of the alternative features
directory.
"""

os.environ['GHERKIN_FEATURES_DIR_NAME'] = 'alternative_features'

self.assert_feature_success(
'alternative_features/subdirectory/another.feature')

del os.environ['GHERKIN_FEATURES_DIR_NAME']

def test_all_alternative_features(self):
"""
Test running all the features under the alternative features directory
without explicitly specifying them.
"""

os.environ['GHERKIN_FEATURES_DIR_NAME'] = 'alternative_features'

result = self.assert_feature_success()

del os.environ['GHERKIN_FEATURES_DIR_NAME']

self.assertEqual(result.tests_run, [
os.path.abspath(feature) for feature in (
'alternative_features/single.feature',
'alternative_features/subdirectory/another.feature',
'submodule/alternative_features/third.feature',
)
])
Empty file.
5 changes: 5 additions & 0 deletions tests/step_definition_app/alternative_features/single.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Feature: Test loading feature from the root features directory

Scenario: Use steps
Given I use alternative step one
And I use alternative step two
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Step module for the test application.
"""
12 changes: 12 additions & 0 deletions tests/step_definition_app/alternative_features/steps/one.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
Part of steps for the tested application.
"""
# pylint:disable=unused-argument

from aloe import step


@step(r'I use alternative step one')
def step_one(self):
"""Dummy step."""
pass
15 changes: 15 additions & 0 deletions tests/step_definition_app/alternative_features/steps/two.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
Part of steps for the tested application.
"""

# pylint:disable=unused-argument,unused-import

from aloe import step

from .one import step_one


@step(r'I use alternative step two')
def step_two(self):
"""Dummy step."""
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Feature: Test loading feature from a subdirectory

Scenario: Use steps
Given I use alternative step one
And I use alternative step two
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Feature: Test loading feature from a submodule

Scenario: Use steps
Given I use alternative step one
And I use alternative step two
17 changes: 16 additions & 1 deletion tests/unit/test_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from __future__ import division
from __future__ import absolute_import

import os
import unittest

from aloe.fs import path_to_module_name
from aloe.exceptions import StepDiscoveryError
from aloe.fs import FeatureLoader, path_to_module_name


class PathToModuleNameTest(unittest.TestCase):
Expand All @@ -29,3 +31,16 @@ def test_path_to_module_name(self):
'one.two',
path_to_module_name('one/two/__init__.py')
)


class AlternativeFeaturesDirTest(unittest.TestCase):
"""Test alternative features directory."""

def test_alternative_directory(self):
"""Test alternative directory is not a path"""

with self.assertRaises(StepDiscoveryError):
os.environ['GHERKIN_FEATURES_DIR_NAME'] = 'my/features'
FeatureLoader.features_dirname()

del os.environ['GHERKIN_FEATURES_DIR_NAME']