Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix a bug in nested workspace and add CI #14

Merged
merged 2 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Rust CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
override: true

- name: Build (required step)
run: cargo build --workspace

- name: Run tests
run: cargo test --verbose --workspace -- --nocapture
45 changes: 7 additions & 38 deletions crates/adapter/src/runner/cargo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use testing_language_server::spec::RunFileTestResultItem;

use crate::model::Runner;

use super::util::detect_workspaces_from_file_paths;

// If the character value is greater than the line length it defaults back to the line length.
const MAX_CHAR_LENGTH: u32 = 10000;

Expand Down Expand Up @@ -181,41 +183,8 @@ fn discover(file_path: &str) -> Result<Vec<TestItem>, LSError> {
Ok(test_items)
}

fn detect_workspace_from_file(file_path: PathBuf) -> Option<String> {
let parent = file_path.parent();
if let Some(parent) = parent {
if parent.join("Cargo.toml").exists() {
return Some(parent.to_string_lossy().to_string());
} else {
detect_workspace_from_file(parent.to_path_buf())
}
} else {
None
}
}

fn detect_workspaces(file_paths: Vec<String>) -> Result<DetectWorkspaceResult, LSError> {
let mut result_map: HashMap<String, Vec<String>> = HashMap::new();
let mut file_paths = file_paths.clone();
file_paths.sort_by_key(|b| std::cmp::Reverse(b.len()));
for file_path in file_paths {
let existing_workspace = result_map
.iter()
.find(|(workspace_root, _)| file_path.contains(workspace_root.as_str()));
if let Some((workspace_root, _)) = existing_workspace {
result_map
.entry(workspace_root.to_string())
.or_default()
.push(file_path);
} else {
let workspace = detect_workspace_from_file(PathBuf::from_str(&file_path).unwrap());
if let Some(workspace) = workspace {
result_map.entry(workspace).or_default().push(file_path);
}
}
}

Ok(result_map)
fn detect_workspaces(file_paths: &[String]) -> DetectWorkspaceResult {
detect_workspaces_from_file_paths(file_paths, &["Cargo.toml".to_string()])
}

#[derive(Eq, PartialEq, Hash, Debug)]
Expand Down Expand Up @@ -263,12 +232,12 @@ impl Runner for CargoTestRunner {
Ok(())
}

fn detect_workspaces_root(
fn detect_workspaces(
&self,
args: testing_language_server::spec::DetectWorkspaceArgs,
) -> Result<(), LSError> {
let file_paths = args.file_paths;
let detect_result = detect_workspaces(file_paths)?;
let detect_result = detect_workspaces(&file_paths);
serde_json::to_writer(std::io::stdout(), &detect_result)?;
Ok(())
}
Expand Down Expand Up @@ -350,7 +319,7 @@ note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
.map(|file_path| file_path.to_str().unwrap().to_string())
.collect();

let workspaces = detect_workspaces(file_paths).unwrap();
let workspaces = detect_workspaces(&file_paths);
assert_eq!(workspaces.len(), 2);
assert!(workspaces.contains_key(&absolute_path_of_test_proj.to_str().unwrap().to_string()));
assert!(workspaces.contains_key(&current_dir.to_str().unwrap().to_string()));
Expand Down
57 changes: 11 additions & 46 deletions crates/adapter/src/runner/jest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use regex::Regex;
use serde_json::Value;
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::str::FromStr;
use tempfile::tempdir;
use testing_language_server::error::LSError;

Expand All @@ -22,6 +20,8 @@ use tree_sitter::QueryCursor;

use crate::model::Runner;

use super::util::detect_workspaces_from_file_paths;

// If the character value is greater than the line length it defaults back to the line length.
const MAX_CHAR_LENGTH: u32 = 10000;

Expand Down Expand Up @@ -81,43 +81,8 @@ fn parse_diagnostics(
.collect())
}

fn detect_workspace_from_file(file_path: PathBuf) -> Option<String> {
let parent = file_path.parent();
if let Some(parent) = parent {
if parent.join("package.json").exists() {
return Some(parent.to_string_lossy().to_string());
} else {
detect_workspace_from_file(parent.to_path_buf())
}
} else {
None
}
}

fn detect_workspaces(file_paths: Vec<String>) -> Result<DetectWorkspaceResult, LSError> {
let mut result_map: HashMap<String, Vec<String>> = HashMap::new();
let mut file_paths: Vec<String> = file_paths
.into_iter()
.filter(|path| !path.contains("node_modules/"))
.collect();
file_paths.sort_by_key(|b| std::cmp::Reverse(b.len()));
for file_path in file_paths {
let existing_workspace = result_map
.iter()
.find(|(workspace_root, _)| file_path.contains(workspace_root.as_str()));
if let Some((workspace_root, _)) = existing_workspace {
result_map
.entry(workspace_root.to_string())
.or_default()
.push(file_path);
} else {
let workspace = detect_workspace_from_file(PathBuf::from_str(&file_path).unwrap());
if let Some(workspace) = workspace {
result_map.entry(workspace).or_default().push(file_path);
}
}
}
Ok(result_map)
fn detect_workspaces(file_paths: Vec<String>) -> DetectWorkspaceResult {
detect_workspaces_from_file_paths(&file_paths, &["package.json".to_string()])
}

fn discover(file_path: &str) -> Result<Vec<TestItem>, LSError> {
Expand Down Expand Up @@ -303,12 +268,12 @@ impl Runner for JestRunner {
Ok(())
}

fn detect_workspaces_root(
fn detect_workspaces(
&self,
args: testing_language_server::spec::DetectWorkspaceArgs,
) -> Result<(), LSError> {
let file_paths = args.file_paths;
let detect_result = detect_workspaces(file_paths)?;
let detect_result = detect_workspaces(file_paths);
serde_json::to_writer(std::io::stdout(), &detect_result)?;
Ok(())
}
Expand Down Expand Up @@ -346,7 +311,7 @@ mod tests {
.iter()
.map(|file_path| file_path.to_str().unwrap().to_string())
.collect();
let detect_result = detect_workspaces(file_paths).unwrap();
let detect_result = detect_workspaces(file_paths);
assert_eq!(detect_result.len(), 1);
detect_result.iter().for_each(|(workspace, _)| {
assert_eq!(workspace, absolute_path_of_test_proj.to_str().unwrap());
Expand All @@ -365,21 +330,21 @@ mod tests {
name: String::from("fail"),
start_position: Range {
start: Position {
line: 2,
line: 1,
character: 2
},
end: Position {
line: 2,
line: 1,
character: MAX_CHAR_LENGTH
}
},
end_position: Range {
start: Position {
line: 4,
line: 3,
character: 0
},
end: Position {
line: 4,
line: 3,
character: 4
}
}
Expand Down
22 changes: 12 additions & 10 deletions crates/adapter/src/runner/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn detect_workspaces_from_file_paths(
) -> HashMap<String, Vec<String>> {
let mut result_map: HashMap<String, Vec<String>> = HashMap::new();
let mut file_paths = target_file_paths.to_vec();
file_paths.sort_by_key(|b| std::cmp::Reverse(b.len()));
file_paths.sort_by_key(|b| b.len());
for file_path in file_paths {
let existing_workspace = result_map
.iter()
Expand All @@ -35,15 +35,17 @@ pub fn detect_workspaces_from_file_paths(
.entry(workspace_root.to_string())
.or_default()
.push(file_path.clone());
} else {
let workspace =
detect_workspace_from_file(PathBuf::from_str(&file_path).unwrap(), file_names);
if let Some(workspace) = workspace {
result_map
.entry(workspace)
.or_default()
.push(file_path.clone());
}
}
// Push the file path to the found workspace even if the existing_workspace becomes Some.
// In some cases, the simple method of finding a workspace, such as the relationship
// between the project root and the adapter crate in this repository, does not work.
let workspace =
detect_workspace_from_file(PathBuf::from_str(&file_path).unwrap(), file_names);
if let Some(workspace) = workspace {
result_map
.entry(workspace)
.or_default()
.push(file_path.clone());
}
}
result_map
Expand Down
Loading