build.plat: in Platform.add_file(), allow adding exact duplicates.
authorwhitequark <cz@m-labs.hk>
Fri, 15 Nov 2019 23:40:44 +0000 (23:40 +0000)
committerwhitequark <cz@m-labs.hk>
Fri, 15 Nov 2019 23:40:44 +0000 (23:40 +0000)
nmigen/build/plat.py
nmigen/test/test_build_plat.py

index 00c147f3060ec03225e36516024aee00cb79e76d..174cda7e7fd76af984a60199ea8b076ebee0f544 100644 (file)
@@ -50,15 +50,17 @@ class Platform(ResourceManager, metaclass=ABCMeta):
         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):
index a974a81219375c85a8ce65e48b5999df210393be..2b2ec62ee72cbfba95332686eb39041e45ffa5c0 100644 (file)
@@ -25,6 +25,10 @@ class PlatformTestCase(FHDLTestCase):
         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)