Normalize line endings consistently between fake and real FS
This commit is contained in:
parent
9804c683c0
commit
c1b886b0ce
1 changed files with 20 additions and 15 deletions
|
@ -174,15 +174,8 @@ impl Fs for RealFs {
|
||||||
let buffer_size = text.summary().bytes.min(10 * 1024);
|
let buffer_size = text.summary().bytes.min(10 * 1024);
|
||||||
let file = smol::fs::File::create(path).await?;
|
let file = smol::fs::File::create(path).await?;
|
||||||
let mut writer = smol::io::BufWriter::with_capacity(buffer_size, file);
|
let mut writer = smol::io::BufWriter::with_capacity(buffer_size, file);
|
||||||
let mut newline = false;
|
for chunk in chunks(text, line_ending) {
|
||||||
for chunk in text.chunks() {
|
writer.write_all(chunk.as_bytes()).await?;
|
||||||
for line in chunk.split('\n') {
|
|
||||||
if newline {
|
|
||||||
writer.write_all(line_ending.as_str().as_bytes()).await?;
|
|
||||||
}
|
|
||||||
writer.write_all(line.as_bytes()).await?;
|
|
||||||
newline = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
writer.flush().await?;
|
writer.flush().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -659,15 +652,12 @@ impl Fs for FakeFs {
|
||||||
let mut state = self.state.lock().await;
|
let mut state = self.state.lock().await;
|
||||||
let path = normalize_path(path);
|
let path = normalize_path(path);
|
||||||
state.validate_path(&path)?;
|
state.validate_path(&path)?;
|
||||||
|
let content = chunks(text, line_ending).collect();
|
||||||
if let Some(entry) = state.entries.get_mut(&path) {
|
if let Some(entry) = state.entries.get_mut(&path) {
|
||||||
if entry.metadata.is_dir {
|
if entry.metadata.is_dir {
|
||||||
Err(anyhow!("cannot overwrite a directory with a file"))
|
Err(anyhow!("cannot overwrite a directory with a file"))
|
||||||
} else {
|
} else {
|
||||||
entry.content = Some(
|
entry.content = Some(content);
|
||||||
text.chunks()
|
|
||||||
.map(|chunk| chunk.replace('\n', line_ending.as_str()))
|
|
||||||
.collect(),
|
|
||||||
);
|
|
||||||
entry.metadata.mtime = SystemTime::now();
|
entry.metadata.mtime = SystemTime::now();
|
||||||
state.emit_event(&[path]).await;
|
state.emit_event(&[path]).await;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -682,7 +672,7 @@ impl Fs for FakeFs {
|
||||||
is_dir: false,
|
is_dir: false,
|
||||||
is_symlink: false,
|
is_symlink: false,
|
||||||
},
|
},
|
||||||
content: Some(text.chunks().collect()),
|
content: Some(content),
|
||||||
};
|
};
|
||||||
state.entries.insert(path.to_path_buf(), entry);
|
state.entries.insert(path.to_path_buf(), entry);
|
||||||
state.emit_event(&[path]).await;
|
state.emit_event(&[path]).await;
|
||||||
|
@ -764,6 +754,21 @@ impl Fs for FakeFs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn chunks(rope: &Rope, line_ending: LineEnding) -> impl Iterator<Item = &str> {
|
||||||
|
rope.chunks().flat_map(move |chunk| {
|
||||||
|
let mut newline = false;
|
||||||
|
chunk.split('\n').flat_map(move |line| {
|
||||||
|
let ending = if newline {
|
||||||
|
Some(line_ending.as_str())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
newline = true;
|
||||||
|
ending.into_iter().chain([line])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn normalize_path(path: &Path) -> PathBuf {
|
pub fn normalize_path(path: &Path) -> PathBuf {
|
||||||
let mut components = path.components().peekable();
|
let mut components = path.components().peekable();
|
||||||
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
|
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue