Skip to content

Commit

Permalink
Fix file based history when capacity is set to zero
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Stan <[email protected]>
  • Loading branch information
andreistan26 committed Dec 29, 2023
1 parent 973dbb5 commit 1bcd2e2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/history/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,17 @@ mod test {

Ok(())
}

#[test]
fn history_size_zero() -> Result<()> {
#[cfg(not(any(feature = "sqlite", feature = "sqlite-dynlib")))]
let mut history = crate::FileBackedHistory::new(0);
history.save(create_item(1, "/home/me", "cd ~/Downloads", 0))?;
assert_eq!(history.count_all()?, 0);
let _ = history.sync();
history.clear()?;
drop(history);

Ok(())
}
}
4 changes: 2 additions & 2 deletions src/history/file_backed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl History for FileBackedHistory {
.entries

Check warning on line 59 in src/history/file_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, bashisms)

Diff in /home/runner/work/reedline/reedline/src/history/file_backed.rs

Check warning on line 59 in src/history/file_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, default)

Diff in /home/runner/work/reedline/reedline/src/history/file_backed.rs

Check warning on line 59 in src/history/file_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, sqlite)

Diff in /home/runner/work/reedline/reedline/src/history/file_backed.rs

Check warning on line 59 in src/history/file_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, basqlite)

Diff in /home/runner/work/reedline/reedline/src/history/file_backed.rs

Check warning on line 59 in src/history/file_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, external_printer)

Diff in /home/runner/work/reedline/reedline/src/history/file_backed.rs
.back()
.map_or(true, |previous| previous != &entry)
&& !entry.is_empty()
&& !entry.is_empty() && self.capacity > 0
{
if self.entries.len() == self.capacity {
// History is "full", so we delete the oldest entry first,
Expand Down Expand Up @@ -234,7 +234,7 @@ impl History for FileBackedHistory {
.collect::<std::io::Result<VecDeque<_>>>()?;

Check warning on line 234 in src/history/file_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, bashisms)

Diff in /home/runner/work/reedline/reedline/src/history/file_backed.rs

Check warning on line 234 in src/history/file_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, default)

Diff in /home/runner/work/reedline/reedline/src/history/file_backed.rs

Check warning on line 234 in src/history/file_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, sqlite)

Diff in /home/runner/work/reedline/reedline/src/history/file_backed.rs

Check warning on line 234 in src/history/file_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, basqlite)

Diff in /home/runner/work/reedline/reedline/src/history/file_backed.rs

Check warning on line 234 in src/history/file_backed.rs

View workflow job for this annotation

GitHub Actions / build-lint-test (ubuntu-latest, stable, external_printer)

Diff in /home/runner/work/reedline/reedline/src/history/file_backed.rs
if from_file.len() + own_entries.len() > self.capacity {
(
from_file.split_off(from_file.len() - (self.capacity - own_entries.len())),
from_file.split_off(from_file.len() - (self.capacity.saturating_sub(own_entries.len()))),
true,
)
} else {
Expand Down

0 comments on commit 1bcd2e2

Please sign in to comment.