Skip to content

Commit 5640c0d

Browse files
committed
rust: file format tests are in a single module
1 parent 1ef4c61 commit 5640c0d

File tree

9 files changed

+812
-709
lines changed

9 files changed

+812
-709
lines changed

rust/bear/src/intercept/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::sync::mpsc::{channel, Receiver, Sender};
1919
use std::sync::Arc;
2020
use std::{env, fmt, thread};
2121

22-
pub mod persistence;
2322
pub mod supervise;
2423
pub mod tcp;
2524

rust/bear/src/intercept/persistence.rs

Lines changed: 0 additions & 129 deletions
This file was deleted.

rust/bear/src/modes/mod.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
pub mod intercept;
44
pub mod semantic;
55

6-
use crate::intercept::persistence;
76
use crate::modes::intercept::BuildInterceptor;
87
use crate::modes::semantic::SemanticAnalysisPipeline;
8+
use crate::output::{ExecutionEventDatabase, FileFormat};
99
use crate::{args, config};
1010
use anyhow::Context;
11-
use std::fs::{File, OpenOptions};
12-
use std::io;
1311
use std::io::BufReader;
1412
use std::process::ExitCode;
13+
use std::{fs, io, path};
1514

1615
/// The mode trait is used to run the application in different modes.
1716
pub trait Mode {
@@ -32,17 +31,13 @@ impl Intercept {
3231
output: args::BuildEvents,
3332
config: config::Main,
3433
) -> anyhow::Result<Self> {
35-
let file_name = output.file_name.as_str();
36-
let output_file = OpenOptions::new()
37-
.write(true)
38-
.create(true)
39-
.truncate(true)
40-
.open(file_name)
34+
let file_name = path::PathBuf::from(output.file_name);
35+
let output_file = fs::File::create(file_name.as_path())
4136
.map(io::BufWriter::new)
4237
.with_context(|| format!("Failed to open file: {:?}", file_name))?;
4338

4439
let interceptor = BuildInterceptor::create(config, move |events| {
45-
persistence::write(output_file, events)
40+
ExecutionEventDatabase::write(output_file, events.iter()).map_err(anyhow::Error::from)
4641
})?;
4742

4843
Ok(Self {
@@ -66,7 +61,8 @@ impl Mode for Intercept {
6661
/// The semantic mode we are deduct the semantic meaning of the
6762
/// executed commands from the build process.
6863
pub struct Semantic {
69-
event_file: BufReader<File>,
64+
event_file: BufReader<fs::File>,
65+
event_file_name: path::PathBuf,
7066
semantic: SemanticAnalysisPipeline,
7167
}
7268

@@ -76,17 +72,16 @@ impl Semantic {
7672
output: args::BuildSemantic,
7773
config: config::Main,
7874
) -> anyhow::Result<Self> {
79-
let file_name = input.file_name.as_str();
80-
let event_file = OpenOptions::new()
81-
.read(true)
82-
.open(file_name)
75+
let event_file_name = path::PathBuf::from(input.file_name);
76+
let event_file = fs::File::open(event_file_name.as_path())
8377
.map(BufReader::new)
84-
.with_context(|| format!("Failed to open file: {:?}", file_name))?;
78+
.with_context(|| format!("Failed to open file: {:?}", event_file_name))?;
8579

8680
let semantic = SemanticAnalysisPipeline::create(output, &config)?;
8781

8882
Ok(Self {
8983
event_file,
84+
event_file_name,
9085
semantic,
9186
})
9287
}
@@ -98,7 +93,10 @@ impl Mode for Semantic {
9893
/// The exit code is based on the result of the output writer.
9994
fn run(self) -> anyhow::Result<ExitCode> {
10095
self.semantic
101-
.analyze_and_write(persistence::read(self.event_file))
96+
.analyze_and_write(ExecutionEventDatabase::read_and_ignore(
97+
self.event_file,
98+
self.event_file_name,
99+
))
102100
.map(|_| ExitCode::SUCCESS)
103101
}
104102
}

rust/bear/src/output/clang/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use crate::output::json;
1515
use serde::Serialize;
1616
use serde_json::Error;
17-
mod tests;
1817
mod type_de;
1918

2019
/// Represents an entry of the compilation database.
@@ -38,6 +37,16 @@ pub struct Entry {
3837
pub output: Option<std::path::PathBuf>,
3938
}
4039

40+
#[cfg(test)]
41+
pub fn entry(file: &str, arguments: Vec<&str>, directory: &str, output: Option<&str>) -> Entry {
42+
Entry {
43+
file: std::path::PathBuf::from(file),
44+
arguments: arguments.into_iter().map(String::from).collect(),
45+
directory: std::path::PathBuf::from(directory),
46+
output: output.map(std::path::PathBuf::from),
47+
}
48+
}
49+
4150
/// Deserialize entries from a JSON array into an iterator.
4251
pub fn read(reader: impl std::io::Read) -> impl Iterator<Item = Result<Entry, Error>> {
4352
json::read_array(reader)

0 commit comments

Comments
 (0)