Skip to content

Commit bb52cd7

Browse files
committed
Add a test
1 parent c649ca2 commit bb52cd7

File tree

1 file changed

+123
-26
lines changed

1 file changed

+123
-26
lines changed

tests/test_rust.py

Lines changed: 123 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,34 @@ def test_separate_crates(self) -> None:
1717
""").encode(),
1818
pathlib.Path('Cargo.toml'): textwrap.dedent("""\
1919
[workspace]
20-
members = ["crates/*/", "verification/"]
20+
members = ["verification"]
21+
22+
[package]
23+
name = "my_competitive_library"
24+
version = "0.0.0"
25+
edition = "2018"
26+
27+
[dependencies]
28+
my-competitive-library-a = { path = "./crates/a" }
29+
my-competitive-library-b = { path = "./crates/b" }
30+
my-competitive-library-c = { path = "./crates/c" }
31+
""").encode(),
32+
pathlib.Path('src', 'lib.rs'): textwrap.dedent("""\
33+
macro_rules! re_export(($($name:ident),* $(,)?) => ($(pub mod $name { pub use $name::*; })*));
34+
re_export!(a, b, c);
2135
""").encode(),
2236
pathlib.Path('crates', 'a', 'Cargo.toml'): textwrap.dedent("""\
2337
[package]
24-
name = "a"
38+
name = "my-competitive-library-a"
2539
version = "0.0.0"
2640
edition = "2018"
2741
42+
[lib]
43+
name = "a"
44+
2845
[dependencies]
29-
b = { path = "../b" }
30-
c = { path = "../c" }
46+
my-competitive-library-b = { path = "../b" }
47+
my-competitive-library-c = { path = "../c" }
3148
""").encode(),
3249
pathlib.Path('crates', 'a', 'src', 'lib.rs'): textwrap.dedent("""\
3350
use b::B;
@@ -37,18 +54,24 @@ def test_separate_crates(self) -> None:
3754
""").encode(),
3855
pathlib.Path('crates', 'b', 'Cargo.toml'): textwrap.dedent("""\
3956
[package]
40-
name = "b"
57+
name = "my-competitive-library-b"
4158
version = "0.0.0"
4259
edition = "2018"
60+
61+
[lib]
62+
name = "b"
4363
""").encode(),
4464
pathlib.Path('crates', 'b', 'src', 'lib.rs'): textwrap.dedent("""\
4565
pub struct B;
4666
""").encode(),
4767
pathlib.Path('crates', 'c', 'Cargo.toml'): textwrap.dedent("""\
4868
[package]
49-
name = "c"
69+
name = "my-competitive-library-c"
5070
version = "0.0.0"
5171
edition = "2018"
72+
73+
[lib]
74+
name = "c"
5275
""").encode(),
5376
pathlib.Path('crates', 'c', 'src', 'lib.rs'): textwrap.dedent("""\
5477
pub struct C;
@@ -60,7 +83,7 @@ def test_separate_crates(self) -> None:
6083
edition = "2018"
6184
6285
[dependencies]
63-
a = { path = "../crates/a" }
86+
my-competitive-library-a = { path = "../crates/a" }
6487
""").encode(),
6588
pathlib.Path('verification', 'src', 'bin', 'aizu-online-judge-itp1-1-a.rs'): textwrap.dedent("""\
6689
// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A
@@ -74,12 +97,28 @@ def test_separate_crates(self) -> None:
7497
}
7598

7699
with tests.utils.load_files_pathlib(files) as tempdir:
77-
expected = sorted(tempdir / 'crates' / name / 'src' / 'lib.rs' for name in ['a', 'b', 'c'])
78-
actual = sorted(RustLanguage(config=None).list_dependencies(tempdir / 'crates' / 'a' / 'src' / 'lib.rs', basedir=tempdir))
100+
101+
def sub_crate_src_path(crate_name: str) -> pathlib.Path:
102+
return tempdir / 'crates' / crate_name / 'src' / 'lib.rs'
103+
104+
top_lib_src_path = tempdir / 'src' / 'lib.rs'
105+
verification_src_path = tempdir / 'verification' / 'src' / 'bin' / 'aizu-online-judge-itp1-1-a.rs'
106+
107+
expected = [sub_crate_src_path('a'), sub_crate_src_path('b'), sub_crate_src_path('c'), top_lib_src_path]
108+
actual = sorted(RustLanguage(config=None).list_dependencies(top_lib_src_path, basedir=tempdir))
109+
self.assertEqual(actual, expected)
110+
111+
expected = [sub_crate_src_path('a'), sub_crate_src_path('b'), sub_crate_src_path('c')]
112+
actual = sorted(RustLanguage(config=None).list_dependencies(sub_crate_src_path('a'), basedir=tempdir))
79113
self.assertEqual(actual, expected)
80114

81-
expected = [tempdir / 'crates' / 'a' / 'src' / 'lib.rs', tempdir / 'verification' / 'src' / 'bin' / 'aizu-online-judge-itp1-1-a.rs']
82-
actual = sorted(RustLanguage(config=None).list_dependencies(tempdir / 'verification' / 'src' / 'bin' / 'aizu-online-judge-itp1-1-a.rs', basedir=tempdir))
115+
for src_path in [sub_crate_src_path('b'), sub_crate_src_path('c')]:
116+
expected = [src_path]
117+
actual = sorted(RustLanguage(config=None).list_dependencies(src_path, basedir=tempdir))
118+
self.assertEqual(actual, expected)
119+
120+
expected = [sub_crate_src_path('a'), verification_src_path]
121+
actual = sorted(RustLanguage(config=None).list_dependencies(verification_src_path, basedir=tempdir))
83122
self.assertEqual(actual, expected)
84123

85124
def test_separate_workspaces(self) -> None:
@@ -89,13 +128,27 @@ def test_separate_workspaces(self) -> None:
89128
""").encode(),
90129
pathlib.Path('Cargo.toml'): textwrap.dedent("""\
91130
[workspace]
92-
members = ["crates/*"]
131+
132+
[package]
133+
name = "my_competitive_library"
134+
version = "0.0.0"
135+
edition = "2018"
136+
137+
[dependencies]
138+
my-competitive-library-a = { path = "./crates/a" }
139+
""").encode(),
140+
pathlib.Path('src', 'lib.rs'): textwrap.dedent("""\
141+
macro_rules! re_export(($($name:ident),* $(,)?) => ($(pub mod $name { pub use $name::*; })*));
142+
re_export!(a);
93143
""").encode(),
94144
pathlib.Path('crates', 'a', 'Cargo.toml'): textwrap.dedent("""\
95145
[package]
96-
name = "a"
146+
name = "my-competitive-library-a"
97147
version = "0.0.0"
98148
edition = "2018"
149+
150+
[lib]
151+
name = "a"
99152
""").encode(),
100153
pathlib.Path('crates', 'a', 'src', 'lib.rs'): b'',
101154
pathlib.Path('verification', 'aizu-online-judge', 'Cargo.toml'): textwrap.dedent("""\
@@ -107,7 +160,7 @@ def test_separate_workspaces(self) -> None:
107160
edition = "2018"
108161
109162
[dependencies]
110-
a = { path = "../../crates/a" }
163+
my-competitive-library-a = { path = "../../crates/a" }
111164
""").encode(),
112165
pathlib.Path('verification', 'aizu-online-judge', 'src', 'bin', 'itp1-1-a.rs'): textwrap.dedent("""\
113166
// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A
@@ -121,15 +174,20 @@ def test_separate_workspaces(self) -> None:
121174
}
122175

123176
with tests.utils.load_files_pathlib(files) as tempdir:
124-
library_path = tempdir / 'crates' / 'a' / 'src' / 'lib.rs'
125-
verification_path = tempdir / 'verification' / 'aizu-online-judge' / 'src' / 'bin' / 'itp1-1-a.rs'
177+
top_lib_src_path = tempdir / 'src' / 'lib.rs'
178+
sub_lib_src_path = tempdir / 'crates' / 'a' / 'src' / 'lib.rs'
179+
verification_src_path = tempdir / 'verification' / 'aizu-online-judge' / 'src' / 'bin' / 'itp1-1-a.rs'
180+
181+
expected = [sub_lib_src_path, top_lib_src_path]
182+
actual = sorted(RustLanguage(config=None).list_dependencies(top_lib_src_path, basedir=tempdir))
183+
self.assertEqual(actual, expected)
126184

127-
expected = [library_path]
128-
actual = sorted(RustLanguage(config=None).list_dependencies(library_path, basedir=tempdir))
185+
expected = [sub_lib_src_path]
186+
actual = sorted(RustLanguage(config=None).list_dependencies(sub_lib_src_path, basedir=tempdir))
129187
self.assertEqual(actual, expected)
130188

131-
expected = [library_path, verification_path]
132-
actual = sorted(RustLanguage(config=None).list_dependencies(verification_path, basedir=tempdir))
189+
expected = [sub_lib_src_path, verification_src_path]
190+
actual = sorted(RustLanguage(config=None).list_dependencies(verification_src_path, basedir=tempdir))
133191
self.assertEqual(actual, expected)
134192

135193
def test_gathered_source_files(self) -> None:
@@ -222,16 +280,28 @@ def test_gathered_source_files(self) -> None:
222280
}
223281

224282
with tests.utils.load_files_pathlib(files) as tempdir:
225-
expected = [*[tempdir / 'crates' / 'sourcefiles' / f'{name}.rs' for name in ['a', 'b', 'c']], tempdir / 'src' / 'lib.rs']
226-
actual = sorted(RustLanguage(config=None).list_dependencies(tempdir / 'src' / 'lib.rs', basedir=tempdir))
283+
284+
def sub_lib_src_path(crate_name: str) -> pathlib.Path:
285+
return tempdir / 'crates' / 'sourcefiles' / f'{crate_name}.rs'
286+
287+
top_lib_src_path = tempdir / 'src' / 'lib.rs'
288+
verification_src_path = tempdir / 'verification' / 'src' / 'bin' / 'aizu-online-judge-itp1-1-a.rs'
289+
290+
expected = [*map(sub_lib_src_path, ['a', 'b', 'c']), top_lib_src_path]
291+
actual = sorted(RustLanguage(config=None).list_dependencies(top_lib_src_path, basedir=tempdir))
227292
self.assertEqual(actual, expected)
228293

229-
expected = [tempdir / 'crates' / 'sourcefiles' / f'{name}.rs' for name in ['a', 'b', 'c']]
230-
actual = sorted(RustLanguage(config=None).list_dependencies(tempdir / 'crates' / 'sourcefiles' / 'a.rs', basedir=tempdir))
294+
expected = list(map(sub_lib_src_path, ['a', 'b', 'c']))
295+
actual = sorted(RustLanguage(config=None).list_dependencies(sub_lib_src_path('a'), basedir=tempdir))
231296
self.assertEqual(actual, expected)
232297

233-
expected = [tempdir / 'crates' / 'sourcefiles' / 'a.rs', tempdir / 'verification' / 'src' / 'bin' / 'aizu-online-judge-itp1-1-a.rs']
234-
actual = sorted(RustLanguage(config=None).list_dependencies(tempdir / 'verification' / 'src' / 'bin' / 'aizu-online-judge-itp1-1-a.rs', basedir=tempdir))
298+
for src_path in map(sub_lib_src_path, ['b', 'c']):
299+
expected = [src_path]
300+
actual = sorted(RustLanguage(config=None).list_dependencies(src_path, basedir=tempdir))
301+
self.assertEqual(actual, expected)
302+
303+
expected = [sub_lib_src_path('a'), verification_src_path]
304+
actual = sorted(RustLanguage(config=None).list_dependencies(verification_src_path, basedir=tempdir))
235305
self.assertEqual(actual, expected)
236306

237307
def test_mono_package(self) -> None:
@@ -275,6 +345,33 @@ def test_mono_package(self) -> None:
275345
actual = sorted(RustLanguage(config=None).list_dependencies(tempdir / 'examples' / 'aizu-online-judge-itp1-1-a.rs', basedir=tempdir))
276346
self.assertEqual(actual, expected)
277347

348+
def test_external_crates(self) -> None:
349+
files = {
350+
pathlib.Path('rust-toolchain'): textwrap.dedent("""\
351+
1.42.0
352+
""").encode(),
353+
pathlib.Path('Cargo.toml'): textwrap.dedent("""\
354+
[package]
355+
name = "my_competitive_library"
356+
version = "0.0.0"
357+
edition = "2018"
358+
359+
[dependencies]
360+
ac-library-rs = { git = "https://github.com/rust-lang-ja/ac-library-rs", rev = "19509cd5103313b884f89b3001510d5155bbb4db" }
361+
maplit = "=1.0.2"
362+
""").encode(),
363+
pathlib.Path('src', 'lib.rs'): textwrap.dedent("""\
364+
use {ac_library_rs as _, maplit as _};
365+
""").encode(),
366+
}
367+
368+
with tests.utils.load_files_pathlib(files) as tempdir:
369+
src_path = tempdir / 'src' / 'lib.rs'
370+
371+
expected = [src_path]
372+
actual = sorted(RustLanguage(config=None).list_dependencies(src_path, basedir=tempdir))
373+
self.assertEqual(actual, expected)
374+
278375
def test_build_dependencies(self) -> None:
279376
files = {
280377
pathlib.Path('rust-toolchain'): textwrap.dedent("""\

0 commit comments

Comments
 (0)