|
| 1 | +use assert_fs::fixture::{FileWriteStr, PathChild}; |
1 | 2 | use assert_fs::TempDir;
|
2 |
| -use indoc::indoc; |
| 3 | +use camino::Utf8PathBuf; |
| 4 | +use indoc::{formatdoc, indoc}; |
| 5 | +use snapbox::assert_matches; |
| 6 | +use std::collections::HashMap; |
| 7 | +use std::path::PathBuf; |
3 | 8 |
|
4 | 9 | use scarb_test_support::command::Scarb;
|
| 10 | +use scarb_test_support::fsx; |
5 | 11 | use scarb_test_support::project_builder::ProjectBuilder;
|
6 | 12 |
|
| 13 | +struct CairoPluginProjectBuilder { |
| 14 | + project: ProjectBuilder, |
| 15 | + src: HashMap<Utf8PathBuf, String>, |
| 16 | +} |
| 17 | + |
| 18 | +impl CairoPluginProjectBuilder { |
| 19 | + pub fn start() -> Self { |
| 20 | + Self { |
| 21 | + project: ProjectBuilder::start(), |
| 22 | + src: Default::default(), |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + pub fn scarb_project(mut self, mutate: impl FnOnce(ProjectBuilder) -> ProjectBuilder) -> Self { |
| 27 | + self.project = mutate(self.project); |
| 28 | + self |
| 29 | + } |
| 30 | + |
| 31 | + pub fn src(mut self, path: impl Into<Utf8PathBuf>, source: impl ToString) -> Self { |
| 32 | + self.src.insert(path.into(), source.to_string()); |
| 33 | + self |
| 34 | + } |
| 35 | + |
| 36 | + pub fn lib_rs(self, source: impl ToString) -> Self { |
| 37 | + self.src("src/lib.rs", source.to_string()) |
| 38 | + } |
| 39 | + |
| 40 | + pub fn just_code(&self, t: &impl PathChild) { |
| 41 | + for (path, source) in &self.src { |
| 42 | + t.child(path).write_str(source).unwrap(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + pub fn build(&self, t: &impl PathChild) { |
| 47 | + self.project.just_manifest(t); |
| 48 | + self.just_code(t); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +fn cairo_lang_macro_lib_path() -> String { |
| 53 | + let path = fsx::canonicalize( |
| 54 | + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../plugins/cairo-lang-macro/"), |
| 55 | + ) |
| 56 | + .unwrap(); |
| 57 | + serde_json::to_string(&path).unwrap() |
| 58 | +} |
| 59 | + |
| 60 | +fn simple_project(t: &impl PathChild) { |
| 61 | + let macro_lib_path = cairo_lang_macro_lib_path(); |
| 62 | + CairoPluginProjectBuilder::start() |
| 63 | + .scarb_project(|b| { |
| 64 | + b.name("hello") |
| 65 | + .version("1.0.0") |
| 66 | + .manifest_extra(r#"[cairo-plugin]"#) |
| 67 | + }) |
| 68 | + .lib_rs(indoc! {r#" |
| 69 | + use cairo_lang_macro::{ProcMacroResult, TokenStream, attribute_macro}; |
| 70 | +
|
| 71 | + #[attribute_macro] |
| 72 | + pub fn some_macro(token_stream: TokenStream) -> ProcMacroResult { |
| 73 | + let _code = token_stream.to_string(); |
| 74 | + ProcMacroResult::Leave |
| 75 | + } |
| 76 | + "#}) |
| 77 | + .src( |
| 78 | + "Cargo.toml", |
| 79 | + formatdoc! {r#" |
| 80 | + [package] |
| 81 | + name = "proc-macro-stub" |
| 82 | + version = "0.1.0" |
| 83 | + edition = "2021" |
| 84 | + publish = false |
| 85 | +
|
| 86 | + [lib] |
| 87 | + crate-type = ["rlib","cdylib"] |
| 88 | +
|
| 89 | + [dependencies] |
| 90 | + cairo-lang-macro = {{ path = {macro_lib_path}}} |
| 91 | + "#}, |
| 92 | + ) |
| 93 | + .build(t); |
| 94 | +} |
| 95 | + |
7 | 96 | #[test]
|
8 |
| -#[ignore = "TODO(maciektr): Remove when proc-macros are implemented."] |
9 | 97 | fn compile_cairo_plugin() {
|
10 | 98 | let t = TempDir::new().unwrap();
|
11 |
| - ProjectBuilder::start() |
12 |
| - .name("hello") |
13 |
| - .version("1.0.0") |
14 |
| - .manifest_extra(r#"[cairo-plugin]"#) |
15 |
| - .build(&t); |
16 |
| - |
17 |
| - Scarb::quick_snapbox() |
18 |
| - .arg("check") |
| 99 | + simple_project(&t); |
| 100 | + let output = Scarb::quick_snapbox() |
| 101 | + .arg("build") |
| 102 | + // Disable colors in Cargo output. |
| 103 | + .env("CARGO_TERM_COLOR", "never") |
19 | 104 | .current_dir(&t)
|
20 |
| - .assert() |
21 |
| - .failure() |
22 |
| - .stdout_matches(indoc! {r#" |
23 |
| - error: compiling Cairo plugin packages is not possible yet |
24 |
| - "#}); |
| 105 | + .output() |
| 106 | + .unwrap(); |
| 107 | + assert!( |
| 108 | + output.status.success(), |
| 109 | + "{}", |
| 110 | + String::from_utf8_lossy(&output.stderr) |
| 111 | + ); |
| 112 | + let stdout = String::from_utf8_lossy(&output.stdout).to_string(); |
| 113 | + assert!(stdout.contains("Compiling hello v1.0.0")); |
| 114 | + let lines = stdout.lines().map(ToString::to_string).collect::<Vec<_>>(); |
| 115 | + let (last, lines) = lines.split_last().unwrap(); |
| 116 | + assert_matches(r#"[..] Finished release target(s) in [..]"#, last); |
| 117 | + let (last, _lines) = lines.split_last().unwrap(); |
| 118 | + // Line from Cargo output |
| 119 | + assert_matches(r#"[..]Finished release [optimized] target(s) in[..]"#, last); |
25 | 120 | }
|
26 | 121 |
|
27 | 122 | #[test]
|
|
0 commit comments