|
| 1 | +mod common; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use assert_cmd::{Command, cargo_bin}; |
| 5 | +use common::create_test_files; |
| 6 | +use predicates::str::contains; |
| 7 | +use std::fs; |
| 8 | +use std::path::PathBuf; |
| 9 | + |
| 10 | +const CUSTOM_LANGUAGE_CONFIG: &str = r#" |
| 11 | +ruleDirs: [] |
| 12 | +customLanguages: |
| 13 | + example: |
| 14 | + libraryPath: missing-library.so |
| 15 | + extensions: [example] |
| 16 | +"#; |
| 17 | + |
| 18 | +#[test] |
| 19 | +fn custom_languages_require_explicit_opt_in() -> Result<()> { |
| 20 | + let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?; |
| 21 | + |
| 22 | + Command::new(cargo_bin!()) |
| 23 | + .current_dir(dir.path()) |
| 24 | + .arg("scan") |
| 25 | + .assert() |
| 26 | + .failure() |
| 27 | + .stderr(contains("Custom language libraries are not allowed")); |
| 28 | + Ok(()) |
| 29 | +} |
| 30 | + |
| 31 | +#[test] |
| 32 | +fn allow_custom_languages_attempts_to_load_the_library() -> Result<()> { |
| 33 | + let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?; |
| 34 | + |
| 35 | + Command::new(cargo_bin!()) |
| 36 | + .current_dir(dir.path()) |
| 37 | + .args(["scan", "--allow-custom-languages"]) |
| 38 | + .assert() |
| 39 | + .failure() |
| 40 | + .stderr(contains("Cannot load custom language library")); |
| 41 | + Ok(()) |
| 42 | +} |
| 43 | + |
| 44 | +#[test] |
| 45 | +fn help_never_loads_custom_languages() -> Result<()> { |
| 46 | + let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?; |
| 47 | + |
| 48 | + Command::new(cargo_bin!()) |
| 49 | + .current_dir(dir.path()) |
| 50 | + .args(["--allow-custom-languages", "--help"]) |
| 51 | + .assert() |
| 52 | + .success() |
| 53 | + .stdout(contains("Search and Rewrite code")); |
| 54 | + Ok(()) |
| 55 | +} |
| 56 | + |
| 57 | +#[test] |
| 58 | +fn completions_never_load_custom_languages() -> Result<()> { |
| 59 | + let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?; |
| 60 | + |
| 61 | + Command::new(cargo_bin!()) |
| 62 | + .current_dir(dir.path()) |
| 63 | + .args(["completions", "bash", "--allow-custom-languages"]) |
| 64 | + .assert() |
| 65 | + .success() |
| 66 | + .stdout(contains("ast-grep")); |
| 67 | + Ok(()) |
| 68 | +} |
| 69 | + |
| 70 | +#[test] |
| 71 | +fn positional_path_does_not_enable_custom_languages() -> Result<()> { |
| 72 | + let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?; |
| 73 | + |
| 74 | + Command::new(cargo_bin!()) |
| 75 | + .current_dir(dir.path()) |
| 76 | + .args(["-p", "foo", "--", "--allow-custom-languages"]) |
| 77 | + .assert() |
| 78 | + .failure() |
| 79 | + .stderr(contains("Custom language libraries are not allowed")); |
| 80 | + Ok(()) |
| 81 | +} |
| 82 | + |
| 83 | +#[test] |
| 84 | +fn denied_custom_languages_do_not_overwrite_project_config() -> Result<()> { |
| 85 | + let dir = create_test_files([("sgconfig.yml", CUSTOM_LANGUAGE_CONFIG)])?; |
| 86 | + |
| 87 | + Command::new(cargo_bin!()) |
| 88 | + .current_dir(dir.path()) |
| 89 | + .args(["new", "project", "--yes"]) |
| 90 | + .assert() |
| 91 | + .failure() |
| 92 | + .stderr(contains("Custom language libraries are not allowed")); |
| 93 | + assert_eq!( |
| 94 | + fs::read_to_string(dir.path().join("sgconfig.yml"))?, |
| 95 | + CUSTOM_LANGUAGE_CONFIG |
| 96 | + ); |
| 97 | + Ok(()) |
| 98 | +} |
| 99 | + |
| 100 | +#[test] |
| 101 | +fn allowed_custom_language_is_registered_before_cli_parsing() -> Result<()> { |
| 102 | + let library = if cfg!(all(target_os = "macos", target_arch = "aarch64")) { |
| 103 | + "json-mac.so" |
| 104 | + } else if cfg!(all(target_os = "linux", target_arch = "x86_64")) { |
| 105 | + "json-linux.so" |
| 106 | + } else { |
| 107 | + return Ok(()); |
| 108 | + }; |
| 109 | + let library = PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 110 | + .join("../../fixtures") |
| 111 | + .join(library); |
| 112 | + let config = format!( |
| 113 | + "customLanguages:\n myjson:\n libraryPath: {}\n languageSymbol: tree_sitter_json\n extensions: [myjson]\n", |
| 114 | + library.display() |
| 115 | + ); |
| 116 | + let dir = create_test_files([("sgconfig.yml", config.as_str())])?; |
| 117 | + |
| 118 | + Command::new(cargo_bin!()) |
| 119 | + .current_dir(dir.path()) |
| 120 | + .args([ |
| 121 | + "run", |
| 122 | + "-p", |
| 123 | + "1", |
| 124 | + "-l", |
| 125 | + "myjson", |
| 126 | + "--allow-custom-languages", |
| 127 | + "--stdin", |
| 128 | + ]) |
| 129 | + .write_stdin(r#"{"key": 1}"#) |
| 130 | + .assert() |
| 131 | + .success(); |
| 132 | + Ok(()) |
| 133 | +} |
0 commit comments