Skip to content

Commit

Permalink
feat: 570 kotlin support (#573)
Browse files Browse the repository at this point in the history
Co-authored-by: Morgante Pell <[email protected]>
  • Loading branch information
Alex-ley-scrub and morgante authored Jan 25, 2025
1 parent 0de0cdb commit dda5543
Show file tree
Hide file tree
Showing 68 changed files with 939,715 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,6 @@
[submodule "resources/language-submodules/tree-sitter-php"]
path = resources/language-submodules/tree-sitter-php
url = https://github.com/tree-sitter/tree-sitter-php
[submodule "resources/language-submodules/tree-sitter-kotlin"]
path = resources/language-submodules/tree-sitter-kotlin
url = https://github.com/fwcd/tree-sitter-kotlin
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions crates/cli_bin/fixtures/kotlin_examples/build.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// https://github.com/actions/runner-images/blob/main/README.md#available-images
val validImages = listOf(
"ubuntu-20.04",
"ubuntu-latest",
"""ubuntu-latest""",
"ubuntu-24.04",
)

fun hi(name: String) = "hi " + name
7 changes: 7 additions & 0 deletions crates/cli_bin/fixtures/python_examples/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://github.com/actions/runner-images/blob/main/README.md#available-images
steps = [
"ubuntu-latest",
'ubuntu-latest',
r"ubuntu-latest",
"""ubuntu-latest""",
]
128 changes: 128 additions & 0 deletions crates/cli_bin/tests/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2852,6 +2852,68 @@ fn apply_stdin_simple() -> Result<()> {
Ok(())
}

/// simple stdin example from documentation but with js array append query/operation
#[test]
fn apply_stdin_js_array_append() -> Result<()> {
let (_temp_dir, fixture_dir) = get_fixture("limit_files", false)?;

let input_file = r#"const allLanguages = ["python", "java",]"#;
let expected_output = r#"const allLanguages = ["python", "java", "kotlin",]"#;

let mut cmd = get_test_cmd()?;
cmd.arg("apply")
.arg(r#"`const allLanguages = [$langs]` where { $langs += `"kotlin",` }"#)
.arg("--stdin")
.arg("--lang")
.arg("js")
.current_dir(&fixture_dir);

cmd.write_stdin(String::from_utf8(input_file.into())?);

let result = cmd.output()?;

let stderr = String::from_utf8(result.stderr)?;
println!("stderr: {:?}", stderr);
let stdout = String::from_utf8(result.stdout)?;
println!("stdout: {:?}", stdout);

assert!(result.status.success(), "Command should have succeeded");
assert!(stdout.contains(expected_output));

Ok(())
}

/// simple stdin example from documentation but with python list append query/operation
#[test]
fn apply_stdin_python_list_append() -> Result<()> {
let (_temp_dir, fixture_dir) = get_fixture("limit_files", false)?;

let input_file = r#"all_languages = ["python", "java"]"#;
let expected_output = r#"all_languages = ["python", "java", "kotlin",]"#;

let mut cmd = get_test_cmd()?;
cmd.arg("apply")
.arg(r#"`all_languages = [$langs]` where { $langs += `"kotlin",` }"#)
.arg("--stdin")
.arg("--lang")
.arg("py")
.current_dir(&fixture_dir);

cmd.write_stdin(String::from_utf8(input_file.into())?);

let result = cmd.output()?;

let stderr = String::from_utf8(result.stderr)?;
println!("stderr: {:?}", stderr);
let stdout = String::from_utf8(result.stdout)?;
println!("stdout: {:?}", stdout);

assert!(result.status.success(), "Command should have succeeded");
assert!(stdout.contains(expected_output));

Ok(())
}

/// simple stdin example from documentation, but using a language alias
#[test]
fn apply_stdin_with_lang_alias() -> Result<()> {
Expand Down Expand Up @@ -3084,6 +3146,72 @@ fn apply_to_yaml_with_multiple_equivalent_strings() -> Result<()> {
Ok(())
}

/// test that we can apply to a kotlin file containing equivalent strings
/// but with different formatting/representations (double quotes vs triple double quotes)
#[test]
fn apply_to_kotlin_with_multiple_equivalent_strings() -> Result<()> {
let (_temp_dir, fixture_dir) = get_fixture("kotlin_examples", false)?;

let mut cmd = get_test_cmd()?;
cmd.arg("apply")
.arg(r#"`"ubuntu-latest"` => `"ubuntu-22.04"`"#)
.arg("build.kt")
.arg("--lang=kotlin")
.arg("--force")
.current_dir(&fixture_dir);

let result = cmd.output()?;

let stderr = String::from_utf8(result.stderr)?;
println!("stderr: {:?}", stderr);
let stdout = String::from_utf8(result.stdout)?;
println!("stdout: {:?}", stdout);

assert!(result.status.success(), "Command failed");
// Read back the build.yml file to ensure it was processed correctly
let target_file = fixture_dir.join("build.kt");
let content: String = fs_err::read_to_string(target_file)?;
assert_snapshot!(content);

// ensure all equivalent strings were replaced
assert!(stdout.contains("Processed 1 files and found 2 matches"));

Ok(())
}

/// test that we can apply to a python file containing equivalent strings
/// but with different formatting/representations (single quotes vs double quotes vs triple double quotes)
#[test]
fn apply_to_python_with_multiple_equivalent_strings() -> Result<()> {
let (_temp_dir, fixture_dir) = get_fixture("python_examples", false)?;

let mut cmd = get_test_cmd()?;
cmd.arg("apply")
.arg(r#"`"ubuntu-latest"` => `"ubuntu-22.04"`"#)
.arg("build.py")
.arg("--lang=python")
.arg("--force")
.current_dir(&fixture_dir);

let result = cmd.output()?;

let stderr = String::from_utf8(result.stderr)?;
println!("stderr: {:?}", stderr);
let stdout = String::from_utf8(result.stdout)?;
println!("stdout: {:?}", stdout);

assert!(result.status.success(), "Command failed");
// Read back the build.yml file to ensure it was processed correctly
let target_file = fixture_dir.join("build.py");
let content: String = fs_err::read_to_string(target_file)?;
assert_snapshot!(content);

// ensure all equivalent strings were replaced
assert!(stdout.contains("Processed 1 files and found 4 matches"));

Ok(())
}

/// Ban multiple stdin paths
#[test]
fn apply_stdin_two_paths() -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: crates/cli_bin/tests/apply.rs
expression: content
---
// https://github.com/actions/runner-images/blob/main/README.md#available-images
val validImages = listOf(
"ubuntu-20.04",
"ubuntu-22.04",
"ubuntu-22.04",
"ubuntu-24.04",
)

fun hi(name: String) = "hi " + name
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: crates/cli_bin/tests/apply.rs
expression: content
---
# https://github.com/actions/runner-images/blob/main/README.md#available-images
steps = [
"ubuntu-22.04",
"ubuntu-22.04",
"ubuntu-22.04",
"ubuntu-22.04",
]
Loading

0 comments on commit dda5543

Please sign in to comment.