Skip to content

Commit

Permalink
build.plat: in Platform.add_file(), allow adding exact duplicates.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Nov 15, 2019
1 parent fe400b5 commit 834fe3c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 6 additions & 4 deletions nmigen/build/plat.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@ def add_file(self, filename, content):
if not isinstance(filename, str):
raise TypeError("File name must be a string, not {!r}"
.format(filename))
if filename in self.extra_files:
raise ValueError("File {!r} already exists"
.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, not {!r}"
.format(content))
self.extra_files[filename] = 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):
Expand Down
4 changes: 4 additions & 0 deletions nmigen/test/test_build_plat.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ 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)
Expand Down

0 comments on commit 834fe3c

Please sign in to comment.