ctdb-scripts: Improve update and listing code
[samba4-gss.git] / python / samba / tests / rust.py
blob99f66a26cd2cf1efd7d04e10476ab3084228b943
1 # Unix SMB/CIFS implementation.
3 # Tests for Rust
5 # Copyright (C) David Mulder <dmulder@samba.org> 2024
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 """Cargo tests for Rust sources"""
23 from samba.tests import TestCase, BlackboxProcessError
24 import os
25 from subprocess import Popen, PIPE
26 from samba import is_rust_built
29 class RustCargoTests(TestCase):
30 def setUp(self):
31 super().setUp()
33 # Locate the rust source directory
34 self.rust_dir = os.path.abspath(
35 os.path.join(
36 os.path.realpath(
37 os.path.dirname(__file__)
39 '../../../../rust'
43 # Locate the bin directory
44 self.target_dir = os.path.abspath(
45 os.path.join(
46 os.path.realpath(
47 os.path.dirname(__file__)
49 '../../..',
50 'default/rust',
54 def check_cargo_test(self, crate_toml):
55 # Execute the cargo test command
56 cmd = 'cargo test --target-dir=%s --manifest-path=%s' % (self.target_dir, crate_toml)
57 p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
58 stdoutdata, stderrdata = p.communicate()
59 retcode = p.returncode
60 if retcode != 0:
61 msg = "cargo test failed; return code %s" % retcode
62 raise BlackboxProcessError(retcode,
63 cmd,
64 stdoutdata.decode('utf-8'),
65 stderrdata.decode('utf-8'),
66 msg)
68 def test_rust(self):
69 if not is_rust_built():
70 self.skipTest('Cannot test Samba Rust if not built')
72 crates = []
73 for root, dirs, files in os.walk(self.rust_dir):
74 for file in files:
75 if os.path.basename(file) == 'Cargo.toml':
76 if root != self.rust_dir:
77 crates.append(os.path.join(root, file))
79 for crate_toml in crates:
80 with self.subTest(crate_toml):
81 self.check_cargo_test(crate_toml)