• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 use camino::{Utf8Path, Utf8PathBuf};
6 use proc_macro::TokenStream;
7 use quote::{format_ident, quote};
8 use std::env;
9 use syn::{parse_macro_input, punctuated::Punctuated, LitStr, Token};
10 
build_foreign_language_testcases(tokens: TokenStream) -> TokenStream11 pub(crate) fn build_foreign_language_testcases(tokens: TokenStream) -> TokenStream {
12     let input = parse_macro_input!(tokens as BuildForeignLanguageTestCaseInput);
13     // we resolve each path relative to the crate root directory.
14     let pkg_dir = env::var("CARGO_MANIFEST_DIR")
15         .expect("Missing $CARGO_MANIFEST_DIR, cannot build tests for generated bindings");
16 
17     // For each test file found, generate a matching testcase.
18     let test_functions = input
19         .test_scripts
20         .iter()
21         .map(|file_path| {
22             let test_file_pathbuf: Utf8PathBuf = [&pkg_dir, file_path].iter().collect();
23             let test_file_path = test_file_pathbuf.to_string();
24             let test_file_name = test_file_pathbuf
25                 .file_name()
26                 .expect("Test file has no name, cannot build tests for generated bindings");
27             let test_name = format_ident!(
28                 "uniffi_foreign_language_testcase_{}",
29                 test_file_name.replace(|c: char| !c.is_alphanumeric(), "_")
30             );
31             let run_test = match test_file_pathbuf.extension() {
32                 Some("kts") => quote! {
33                     uniffi::kotlin_run_test
34                 },
35                 Some("swift") => quote! {
36                     uniffi::swift_run_test
37                 },
38                 Some("py") => quote! {
39                     uniffi::python_run_test
40                 },
41                 Some("rb") => quote! {
42                     uniffi::ruby_run_test
43                 },
44                 _ => panic!("Unexpected extension for test script: {test_file_name}"),
45             };
46             let maybe_ignore = if should_skip_path(&test_file_pathbuf) {
47                 quote! { #[ignore] }
48             } else {
49                 quote! {}
50             };
51             quote! {
52                 #maybe_ignore
53                 #[test]
54                 fn #test_name () -> uniffi::deps::anyhow::Result<()> {
55                     #run_test(
56                         std::env!("CARGO_TARGET_TMPDIR"),
57                         std::"uniffi_macros",
58                         #test_file_path)
59                 }
60             }
61         })
62         .collect::<Vec<proc_macro2::TokenStream>>();
63     let test_module = quote! {
64         #(#test_functions)*
65     };
66     TokenStream::from(test_module)
67 }
68 
69 // UNIFFI_TESTS_DISABLE_EXTENSIONS contains a comma-sep'd list of extensions (without leading `.`)
should_skip_path(path: &Utf8Path) -> bool70 fn should_skip_path(path: &Utf8Path) -> bool {
71     let ext = path.extension().expect("File has no extension!");
72     env::var("UNIFFI_TESTS_DISABLE_EXTENSIONS")
73         .map(|v| v.split(',').any(|look| look == ext))
74         .unwrap_or(false)
75 }
76 
77 struct BuildForeignLanguageTestCaseInput {
78     test_scripts: Vec<String>,
79 }
80 
81 impl syn::parse::Parse for BuildForeignLanguageTestCaseInput {
parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self>82     fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
83         let test_scripts = Punctuated::<LitStr, Token![,]>::parse_terminated(input)?
84             .iter()
85             .map(|s| s.value())
86             .collect();
87 
88         Ok(Self { test_scripts })
89     }
90 }
91