Bug 1319156 - part 3 - tests for Rust library features, frontend and backend; r=chmanchester
authorNathan Froyd <froydnj@mozilla.com>
Fri, 02 Dec 2016 11:39:15 -0500
changeset 325174 db686076ae29249df47ff792c996de293d23cd6f
parent 325173 b5de6b777970f98b97e9f33db1e7e8b4a7deeeab
child 325175 a7ab3fbb933fb1ea7a0f6d5809a663d8242fab9b
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewerschmanchester
bugs1319156
milestone53.0a1
Bug 1319156 - part 3 - tests for Rust library features, frontend and backend; r=chmanchester This commit also adds an overdue test for plain Rust libraries in the recursivemake backend, but said test also serves to ensure that we don't emit features for a library if none were defined in moz.build.
python/mozbuild/mozbuild/test/backend/common.py
python/mozbuild/mozbuild/test/backend/data/rust-library-features/Cargo.toml
python/mozbuild/mozbuild/test/backend/data/rust-library-features/moz.build
python/mozbuild/mozbuild/test/backend/data/rust-library/Cargo.toml
python/mozbuild/mozbuild/test/backend/data/rust-library/moz.build
python/mozbuild/mozbuild/test/backend/test_recursivemake.py
python/mozbuild/mozbuild/test/frontend/data/rust-library-duplicate-features/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/rust-library-duplicate-features/moz.build
python/mozbuild/mozbuild/test/frontend/data/rust-library-features/Cargo.toml
python/mozbuild/mozbuild/test/frontend/data/rust-library-features/moz.build
python/mozbuild/mozbuild/test/frontend/test_emitter.py
--- a/python/mozbuild/mozbuild/test/backend/common.py
+++ b/python/mozbuild/mozbuild/test/backend/common.py
@@ -47,16 +47,36 @@ CONFIGS = defaultdict(lambda: {
         'defines': {},
         'non_global_defines': [],
         'substs': {
             'LIB_PREFIX': 'lib',
             'LIB_SUFFIX': 'a',
             'COMPILE_ENVIRONMENT': '1',
         },
     },
+    'rust-library': {
+        'defines': {},
+        'non_global_defines': [],
+        'substs': {
+            'COMPILE_ENVIRONMENT': '1',
+            'RUST_TARGET': 'x86_64-unknown-linux-gnu',
+            'LIB_PREFIX': 'lib',
+            'LIB_SUFFIX': 'a',
+        },
+    },
+    'rust-library-features': {
+        'defines': {},
+        'non_global_defines': [],
+        'substs': {
+            'COMPILE_ENVIRONMENT': '1',
+            'RUST_TARGET': 'x86_64-unknown-linux-gnu',
+            'LIB_PREFIX': 'lib',
+            'LIB_SUFFIX': 'a',
+        },
+    },
     'rust-programs': {
         'defines': {},
         'non_global_defines': [],
         'substs': {
             'COMPILE_ENVIRONMENT': '1',
             'RUST_TARGET': 'i686-pc-windows-msvc',
             'RUST_HOST_TARGET': 'i686-pc-windows-msvc',
             'BIN_SUFFIX': '.exe',
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/backend/data/rust-library-features/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "gkrust"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[lib]
+crate-type = ["staticlib"]
+
+[profile.dev]
+panic = "abort"
+
+[profile.release]
+panic = "abort"
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/backend/data/rust-library-features/moz.build
@@ -0,0 +1,19 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name, features):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+    RUST_LIBRARY_FEATURES = features
+
+
+RustLibrary('gkrust', ['musthave', 'cantlivewithout'])
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/backend/data/rust-library/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "gkrust"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[lib]
+crate-type = ["staticlib"]
+
+[profile.dev]
+panic = "abort"
+
+[profile.release]
+panic = "abort"
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/backend/data/rust-library/moz.build
@@ -0,0 +1,18 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+
+
+RustLibrary('gkrust')
--- a/python/mozbuild/mozbuild/test/backend/test_recursivemake.py
+++ b/python/mozbuild/mozbuild/test/backend/test_recursivemake.py
@@ -730,16 +730,45 @@ class TestRecursiveMakeBackend(BackendTe
         expected = [
             'LOCAL_INCLUDES += -I$(CURDIR)/bar/baz',
             'LOCAL_INCLUDES += -I$(CURDIR)/foo',
         ]
 
         found = [str for str in lines if str.startswith('LOCAL_INCLUDES')]
         self.assertEqual(found, expected)
 
+    def test_rust_library(self):
+        """Test that a Rust library is written to backend.mk correctly."""
+        env = self._consume('rust-library', RecursiveMakeBackend)
+
+        backend_path = mozpath.join(env.topobjdir, 'backend.mk')
+        lines = [l.strip() for l in open(backend_path, 'rt').readlines()[2:]]
+
+        expected = [
+            'RUST_LIBRARY_FILE := x86_64-unknown-linux-gnu/release/libgkrust.a',
+            'CARGO_FILE := $(srcdir)/Cargo.toml',
+        ]
+
+        self.assertEqual(lines, expected)
+
+    def test_rust_library_with_features(self):
+        """Test that a Rust library with features is written to backend.mk correctly."""
+        env = self._consume('rust-library-features', RecursiveMakeBackend)
+
+        backend_path = mozpath.join(env.topobjdir, 'backend.mk')
+        lines = [l.strip() for l in open(backend_path, 'rt').readlines()[2:]]
+
+        expected = [
+            'RUST_LIBRARY_FILE := x86_64-unknown-linux-gnu/release/libgkrust.a',
+            'CARGO_FILE := $(srcdir)/Cargo.toml',
+            'RUST_LIBRARY_FEATURES := musthave cantlivewithout',
+        ]
+
+        self.assertEqual(lines, expected)
+
     def test_rust_programs(self):
         """Test that {HOST_,}RUST_PROGRAMS are written to backend.mk correctly."""
         env = self._consume('rust-programs', RecursiveMakeBackend)
 
         backend_path = mozpath.join(env.topobjdir, 'backend.mk')
         lines = [l.strip() for l in open(backend_path, 'rt').readlines()[2:]]
 
         expected = [
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/rust-library-duplicate-features/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[lib]
+crate-type = ["staticlib"]
+
+[profile.dev]
+panic = "abort"
+
+[profile.release]
+panic = "abort"
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/rust-library-duplicate-features/moz.build
@@ -0,0 +1,19 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name, features):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+    RUST_LIBRARY_FEATURES = features
+
+
+RustLibrary('random-crate', ['musthave', 'cantlivewithout', 'musthave'])
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/rust-library-features/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "random-crate"
+version = "0.1.0"
+authors = [
+  "Nobody <nobody@mozilla.org>",
+]
+
+[lib]
+crate-type = ["staticlib"]
+
+[profile.dev]
+panic = "abort"
+
+[profile.release]
+panic = "abort"
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/test/frontend/data/rust-library-features/moz.build
@@ -0,0 +1,19 @@
+# Any copyright is dedicated to the Public Domain.
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+@template
+def Library(name):
+    '''Template for libraries.'''
+    LIBRARY_NAME = name
+
+
+@template
+def RustLibrary(name, features):
+    '''Template for Rust libraries.'''
+    Library(name)
+
+    IS_RUST_LIBRARY = True
+    RUST_LIBRARY_FEATURES = features
+
+
+RustLibrary('random-crate', ['musthave', 'cantlivewithout'])
--- a/python/mozbuild/mozbuild/test/frontend/test_emitter.py
+++ b/python/mozbuild/mozbuild/test/frontend/test_emitter.py
@@ -1078,16 +1078,33 @@ class TestEmitterBasic(unittest.TestCase
     def test_multiple_rust_libraries(self):
         '''Test that linking multiple Rust libraries throws an error'''
         reader = self.reader('multiple-rust-libraries',
                              extra_substs=dict(RUST_TARGET='i686-pc-windows-msvc'))
         with self.assertRaisesRegexp(LinkageMultipleRustLibrariesError,
              'Cannot link multiple Rust libraries'):
             self.read_topsrcdir(reader)
 
+    def test_rust_library_features(self):
+        '''Test that RustLibrary features are correctly emitted.'''
+        reader = self.reader('rust-library-features',
+                             extra_substs=dict(RUST_TARGET='i686-pc-windows-msvc'))
+        objs = self.read_topsrcdir(reader)
+        self.assertEqual(len(objs), 1)
+        lib = objs[0]
+        self.assertIsInstance(lib, RustLibrary)
+        self.assertEqual(lib.features, ['musthave', 'cantlivewithout'])
+
+    def test_rust_library_duplicate_features(self):
+        '''Test that duplicate RustLibrary features are rejected.'''
+        reader = self.reader('rust-library-duplicate-features')
+        with self.assertRaisesRegexp(SandboxValidationError,
+             'features for .* should not contain duplicates'):
+            self.read_topsrcdir(reader)
+
     def test_rust_program_no_cargo_toml(self):
         '''Test that specifying RUST_PROGRAMS without a Cargo.toml fails.'''
         reader = self.reader('rust-program-no-cargo-toml')
         with self.assertRaisesRegexp(SandboxValidationError,
              'No Cargo.toml file found'):
             self.read_topsrcdir(reader)
 
     def test_host_rust_program_no_cargo_toml(self):