Skip to content

Commit

Permalink
fix(rust_indexer): rustfmt everything (#4941)
Browse files Browse the repository at this point in the history
  • Loading branch information
zrlk committed May 20, 2021
1 parent e77b26d commit 82504c0
Show file tree
Hide file tree
Showing 22 changed files with 420 additions and 170 deletions.
5 changes: 4 additions & 1 deletion kythe/rust/extractor/src/bin/cli.rs
Expand Up @@ -26,7 +26,10 @@ pub struct ExtractorConfig {
impl ExtractorConfig {
/// Create a new ExtractorConfig using the supplied parameters
pub fn new(extra_action_path: PathBuf, output_path: PathBuf) -> Self {
Self { extra_action_path, output_path }
Self {
extra_action_path,
output_path,
}
}
}

Expand Down
21 changes: 16 additions & 5 deletions kythe/rust/extractor/src/bin/extractor.rs
Expand Up @@ -44,8 +44,12 @@ fn main() -> Result<()> {
)?;

// Create the output kzip
let kzip_file = File::create(&config.output_path)
.with_context(|| format!("Failed to create kzip file at path {:?}", config.output_path))?;
let kzip_file = File::create(&config.output_path).with_context(|| {
format!(
"Failed to create kzip file at path {:?}",
config.output_path
)
})?;
let mut kzip = ZipWriter::new(kzip_file);
kzip.add_directory("root/", FileOptions::default())?;

Expand Down Expand Up @@ -116,12 +120,15 @@ fn get_spawn_info(file_path: impl AsRef<Path>) -> Result<SpawnInfo> {
let mut file = File::open(file_path).context("Failed to open extra action file")?;

let mut file_contents_bytes = Vec::new();
file.read_to_end(&mut file_contents_bytes).context("Failed to read extra action file")?;
file.read_to_end(&mut file_contents_bytes)
.context("Failed to read extra action file")?;

let extra_action = protobuf::parse_from_bytes::<ExtraActionInfo>(&file_contents_bytes)
.context("Failed to parse extra action protobuf")?;

SPAWN_INFO.get(&extra_action).ok_or_else(|| anyhow!("SpawnInfo extension missing"))
SPAWN_INFO
.get(&extra_action)
.ok_or_else(|| anyhow!("SpawnInfo extension missing"))
}

/// Create an IndexedCompilation protobuf from the supplied arguments
Expand Down Expand Up @@ -183,7 +190,11 @@ fn kzip_add_required_input(
.read_to_end(&mut source_file_contents)
.with_context(|| format!("Failed read file {:?}", file_path_string))?;
let digest = sha256digest(&source_file_contents);
kzip_add_file(format!("root/files/{}", digest), &source_file_contents, zip_writer)?;
kzip_add_file(
format!("root/files/{}", digest),
&source_file_contents,
zip_writer,
)?;

// Generate FileInput and add it to the list of required inputs
let mut file_input = CompilationUnit_FileInput::new();
Expand Down
12 changes: 9 additions & 3 deletions kythe/rust/extractor/src/bin/save_analysis.rs
Expand Up @@ -48,9 +48,15 @@ fn generate_arguments(arguments: Vec<String>, output_dir: &Path) -> Result<Vec<S
let outdir_position = rustc_arguments
.iter()
.position(|arg| arg.contains("--out-dir"))
.ok_or_else(|| anyhow!("Could not find the output directory argument: {:?}", arguments))?;
let outdir_str =
output_dir.to_str().ok_or_else(|| anyhow!("Couldn't convert temporary path to string"))?;
.ok_or_else(|| {
anyhow!(
"Could not find the output directory argument: {:?}",
arguments
)
})?;
let outdir_str = output_dir
.to_str()
.ok_or_else(|| anyhow!("Couldn't convert temporary path to string"))?;
rustc_arguments[outdir_position] = format!("--out-dir={}", outdir_str);

Ok(rustc_arguments)
Expand Down
5 changes: 3 additions & 2 deletions kythe/rust/extractor/src/lib.rs
Expand Up @@ -30,8 +30,9 @@ use std::path::PathBuf;
/// The save_analysis JSON output file will be located at
/// {output_dir}/save-analysis/{crate_name}.json
pub fn generate_analysis(rustc_arguments: Vec<String>, output_dir: PathBuf) -> Result<(), String> {
let first_arg =
rustc_arguments.get(0).ok_or_else(|| "Arguments vector should not be empty".to_string())?;
let first_arg = rustc_arguments
.get(0)
.ok_or_else(|| "Arguments vector should not be empty".to_string())?;
if first_arg != &"".to_string() {
return Err("The first argument must be an empty string".into());
}
Expand Down
53 changes: 42 additions & 11 deletions kythe/rust/extractor/tests/integration_test.rs
Expand Up @@ -86,7 +86,12 @@ fn main() -> Result<()> {

missing_arguments_fail();
bad_extra_action_path_fails();
correct_arguments_succeed(&extra_action_path_str, &temp_dir_str, &output_key, arguments);
correct_arguments_succeed(
&extra_action_path_str,
&temp_dir_str,
&output_key,
arguments,
);

Ok(())
}
Expand All @@ -98,18 +103,24 @@ fn missing_arguments_fail() {
.assert()
.failure()
.code(1)
.stderr(starts_with("error: The following required arguments were not provided:"));
.stderr(starts_with(
"error: The following required arguments were not provided:",
));
Command::new(&extractor_path)
.arg("--extra_action=/tmp/wherever")
.assert()
.failure()
.code(1)
.stderr(starts_with("error: The following required arguments were not provided:"));
.stderr(starts_with(
"error: The following required arguments were not provided:",
));
Command::new(&extractor_path)
.assert()
.failure()
.code(1)
.stderr(starts_with("error: The following required arguments were not provided:"));
.stderr(starts_with(
"error: The following required arguments were not provided:",
));
}

fn bad_extra_action_path_fails() {
Expand Down Expand Up @@ -154,7 +165,10 @@ fn correct_arguments_succeed(
cu_path_str = file.name().to_string();
}
}
assert_ne!(cu_path_str, "", "IndexedCompilation protobuf missing from kzip");
assert_ne!(
cu_path_str, "",
"IndexedCompilation protobuf missing from kzip"
);

// Read it into an IndexedCompilation
let cu_file = kzip.by_name(&cu_path_str).unwrap();
Expand All @@ -173,24 +187,41 @@ fn correct_arguments_succeed(
);

let output_key = compilation_unit.get_output_key();
assert_eq!(output_key, expected_output_key, "output_key field doesn't match");
assert_eq!(
output_key, expected_output_key,
"output_key field doesn't match"
);

let unit_vname = compilation_unit.get_v_name();
assert_eq!(unit_vname.get_language(), "rust", "VName language field doesn't match");
assert_eq!(
unit_vname.get_language(),
"rust",
"VName language field doesn't match"
);

let arguments = compilation_unit.get_argument();
assert_eq!(arguments, expected_arguments, "Argument field doesn't match");
assert_eq!(
arguments, expected_arguments,
"Argument field doesn't match"
);

let required_inputs = compilation_unit.get_required_input().to_vec();
let source_input = required_inputs.get(0).expect("Failed to get first required input");
let source_input = required_inputs
.get(0)
.expect("Failed to get first required input");
assert_eq!(source_input.get_v_name().get_corpus(), "test_corpus");
assert_eq!(source_input.get_info().get_path(), "kythe/rust/extractor/main.rs");
assert_eq!(
source_input.get_info().get_path(),
"kythe/rust/extractor/main.rs"
);
assert_eq!(
source_input.get_info().get_digest(),
"7cb3b3c74ecdf86f434548ba15c1651c92bf03b6690fd0dfc053ab09d094cf03"
);

let analysis_input = required_inputs.get(1).expect("Failed to get second required input");
let analysis_input = required_inputs
.get(1)
.expect("Failed to get second required input");
assert_eq!(analysis_input.get_v_name().get_corpus(), "test_corpus");
let analysis_path = analysis_input.get_info().get_path();
assert!(
Expand Down
10 changes: 8 additions & 2 deletions kythe/rust/extractor/tests/test.rs
Expand Up @@ -25,7 +25,10 @@ fn empty_args_fails() {
let temp_dir = TempDir::new("extractor_test").expect("Could not create temporary directory");
let analysis_directory = PathBuf::new().join(temp_dir.path());
let result = generate_analysis(args, analysis_directory);
assert_eq!(result.unwrap_err(), "Arguments vector should not be empty".to_string());
assert_eq!(
result.unwrap_err(),
"Arguments vector should not be empty".to_string()
);
}

#[test]
Expand All @@ -34,7 +37,10 @@ fn nonempty_string_first_fails() {
let temp_dir = TempDir::new("extractor_test").expect("Could not create temporary directory");
let analysis_directory = PathBuf::new().join(temp_dir.path());
let result = generate_analysis(args, analysis_directory);
assert_eq!(result.unwrap_err(), "The first argument must be an empty string".to_string());
assert_eq!(
result.unwrap_err(),
"The first argument must be an empty string".to_string()
);
}

#[test]
Expand Down

0 comments on commit 82504c0

Please sign in to comment.