Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: m-labs/nmigen
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: f8f7d8312773
Choose a base ref
...
head repository: m-labs/nmigen
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 834fe3c700e4
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Nov 15, 2019

  1. test: add tests for build.plat.Platform.add_file.

    whitequark committed Nov 15, 2019
    Copy the full SHA
    fe400b5 View commit details
  2. Copy the full SHA
    834fe3c View commit details
Showing with 62 additions and 6 deletions.
  1. +10 −6 nmigen/build/plat.py
  2. +52 −0 nmigen/test/test_build_plat.py
16 changes: 10 additions & 6 deletions nmigen/build/plat.py
Original file line number Diff line number Diff line change
@@ -48,15 +48,19 @@ def default_clk_frequency(self):

def add_file(self, filename, content):
if not isinstance(filename, str):
raise TypeError("File name must be a string")
if filename in self.extra_files:
raise ValueError("File {} already exists"
.format(filename))
raise TypeError("File name must be a string, not {!r}"
.format(filename))
if hasattr(content, "read"):
content = content.read()
elif not isinstance(content, (str, bytes)):
raise TypeError("File contents must be str, bytes, or a file-like object")
self.extra_files[filename] = content
raise TypeError("File contents must be str, bytes, or a file-like object, not {!r}"
.format(content))
if filename in self.extra_files:
if self.extra_files[filename] != content:
raise ValueError("File {!r} already exists"
.format(filename))
else:
self.extra_files[filename] = content

@property
def _toolchain_env_var(self):
52 changes: 52 additions & 0 deletions nmigen/test/test_build_plat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from .. import *
from ..build.plat import *
from .utils import *


class MockPlatform(Platform):
resources = []
connectors = []

required_tools = []

def toolchain_prepare(self, fragment, name, **kwargs):
raise NotImplementedError


class PlatformTestCase(FHDLTestCase):
def setUp(self):
self.platform = MockPlatform()

def test_add_file_str(self):
self.platform.add_file("x.txt", "foo")
self.assertEqual(self.platform.extra_files["x.txt"], "foo")

def test_add_file_bytes(self):
self.platform.add_file("x.txt", b"foo")
self.assertEqual(self.platform.extra_files["x.txt"], b"foo")

def test_add_file_exact_duplicate(self):
self.platform.add_file("x.txt", b"foo")
self.platform.add_file("x.txt", b"foo")

def test_add_file_io(self):
with open(__file__) as f:
self.platform.add_file("x.txt", f)
with open(__file__) as f:
self.assertEqual(self.platform.extra_files["x.txt"], f.read())

def test_add_file_wrong_filename(self):
with self.assertRaises(TypeError,
msg="File name must be a string, not 1"):
self.platform.add_file(1, "")

def test_add_file_wrong_contents(self):
with self.assertRaises(TypeError,
msg="File contents must be str, bytes, or a file-like object, not 1"):
self.platform.add_file("foo", 1)

def test_add_file_wrong_duplicate(self):
self.platform.add_file("foo", "")
with self.assertRaises(ValueError,
msg="File 'foo' already exists"):
self.platform.add_file("foo", "bar")