-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from seddonym/error-on-invalid-containers
Error on invalid containers
- Loading branch information
Showing
30 changed files
with
148 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
tests/assets/singlecontractfile/layers_with_missing_container.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Contract A: | ||
containers: | ||
- singlecontractfile.foo | ||
- singlecontractfile.missing | ||
- singlecontractfile.bar | ||
layers: | ||
- one | ||
- two |
File renamed without changes.
Empty file.
Empty file.
10 changes: 10 additions & 0 deletions
10
tests/assets/successpackage/layers_with_missing_container.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Modular: | ||
containers: | ||
- successpackage.one | ||
- successpackage.two | ||
- successpackage.missing | ||
- successpackage.three | ||
layers: | ||
- gamma | ||
- beta | ||
- alpha |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,70 @@ | ||
import os | ||
import sys | ||
|
||
import pytest | ||
|
||
from layer_linter.contract import get_contracts, Layer | ||
from layer_linter.dependencies import ImportPath | ||
from layer_linter.module import Module | ||
|
||
|
||
def test_get_contracts(): | ||
dirname = os.path.dirname(__file__) | ||
filename = os.path.join(dirname, '..', 'assets', 'singlecontractfile', 'layers.yml') | ||
|
||
contracts = get_contracts(filename) | ||
|
||
assert len(contracts) == 2 | ||
expected_contracts = [ | ||
{ | ||
'name': 'Contract A', | ||
'packages': ['foo', 'bar'], | ||
'layers': ['one', 'two'], | ||
}, | ||
{ | ||
'name': 'Contract B', | ||
'packages': ['baz/*'], | ||
'layers': ['one', 'two', 'three'], | ||
'whitelisted_paths': [ | ||
('baz.utils', 'baz.three.green'), | ||
('baz.three.blue', 'baz.two'), | ||
], | ||
}, | ||
] | ||
sorted_contracts = sorted(contracts, key=lambda i: i.name) | ||
for contract_index, contract in enumerate(sorted_contracts): | ||
expected_data = expected_contracts[contract_index] | ||
assert contract.name == expected_data['name'] | ||
|
||
for package_index, package in enumerate(contract.containers): | ||
expected_package_name = expected_data['packages'][package_index] | ||
assert package == Module(expected_package_name) | ||
|
||
for layer_index, layer in enumerate(contract.layers): | ||
expected_layer_data = expected_data['layers'][layer_index] | ||
assert isinstance(layer, Layer) | ||
assert layer.name == expected_layer_data | ||
|
||
for whitelisted_index, whitelisted_path in enumerate(contract.whitelisted_paths): | ||
expected_importer, expected_imported = expected_data['whitelisted_paths'][ | ||
whitelisted_index] | ||
assert isinstance(whitelisted_path, ImportPath) | ||
assert whitelisted_path.importer == Module(expected_importer) | ||
assert whitelisted_path.imported == Module(expected_imported) | ||
class TestGetContracts: | ||
def test_happy_path(self): | ||
self._initialize_test() | ||
|
||
contracts = get_contracts(self.filename_and_path, package_name='singlecontractfile') | ||
|
||
assert len(contracts) == 2 | ||
expected_contracts = [ | ||
{ | ||
'name': 'Contract A', | ||
'packages': ['singlecontractfile.foo', 'singlecontractfile.bar'], | ||
'layers': ['one', 'two'], | ||
}, | ||
{ | ||
'name': 'Contract B', | ||
'packages': ['singlecontractfile'], | ||
'layers': ['one', 'two', 'three'], | ||
'whitelisted_paths': [ | ||
('baz.utils', 'baz.three.green'), | ||
('baz.three.blue', 'baz.two'), | ||
], | ||
}, | ||
] | ||
sorted_contracts = sorted(contracts, key=lambda i: i.name) | ||
for contract_index, contract in enumerate(sorted_contracts): | ||
expected_data = expected_contracts[contract_index] | ||
assert contract.name == expected_data['name'] | ||
|
||
for package_index, package in enumerate(contract.containers): | ||
expected_package_name = expected_data['packages'][package_index] | ||
assert package == Module(expected_package_name) | ||
|
||
for layer_index, layer in enumerate(contract.layers): | ||
expected_layer_data = expected_data['layers'][layer_index] | ||
assert isinstance(layer, Layer) | ||
assert layer.name == expected_layer_data | ||
|
||
for whitelisted_index, whitelisted_path in enumerate(contract.whitelisted_paths): | ||
expected_importer, expected_imported = expected_data['whitelisted_paths'][ | ||
whitelisted_index] | ||
assert isinstance(whitelisted_path, ImportPath) | ||
assert whitelisted_path.importer == Module(expected_importer) | ||
assert whitelisted_path.imported == Module(expected_imported) | ||
|
||
def test_container_does_not_exist(self): | ||
self._initialize_test('layers_with_missing_container.yml') | ||
|
||
with pytest.raises(ValueError) as e: | ||
get_contracts(self.filename_and_path, package_name='singlecontractfile') | ||
|
||
assert str(e.value) == "Invalid container 'singlecontractfile.missing': no such package." | ||
|
||
def _initialize_test(self, config_filename='layers.yml'): | ||
# Append the package directory to the path. | ||
dirname = os.path.dirname(__file__) | ||
package_dirname = os.path.join(dirname, '..', 'assets', 'singlecontractfile') | ||
sys.path.append(package_dirname) | ||
|
||
# Set the full config filename and path as an instance attribute. | ||
self.filename_and_path = os.path.join(package_dirname, config_filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters