Skip to content

Commit

Permalink
Replace make with xtask
Browse files Browse the repository at this point in the history
  • Loading branch information
ajeetdsouza committed Sep 12, 2021
1 parent 61cfb02 commit 02029ae
Show file tree
Hide file tree
Showing 16 changed files with 267 additions and 126 deletions.
2 changes: 2 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
xtask = "run --manifest-path ./xtask/Cargo.toml --"
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
if: ${{ matrix.os != 'windows-latest' }}
with:
nix_path: nixpkgs=https://github.com/NixOS/nixpkgs/archive/20.09.tar.gz
- run: make test
- run: cargo xtask ci --nix ${{ matrix.os != 'windows-latest' }}
39 changes: 0 additions & 39 deletions Makefile

This file was deleted.

1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# group_imports = "StdExternalCrate"
# imports_granularity = "Module"
max_width = 120
newline_style = "Native"
use_field_init_shorthand = true
use_small_heuristics = "Max"
Expand Down
2 changes: 1 addition & 1 deletion src/app/_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ENV_HELP: &str = "ENVIRONMENT VARIABLES:
_ZO_MAXAGE Maximum total age after which entries start getting deleted
_ZO_RESOLVE_SYMLINKS Resolve symlinks when storing paths";

#[derive(Debug, Clap)]
#[derive(Clap, Debug)]
#[clap(
bin_name = env!("CARGO_PKG_NAME"),
about,
Expand Down
6 changes: 1 addition & 5 deletions src/app/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ impl Run for Add {
let mut db = db.open()?;

for path in &self.paths {
let path = if config::resolve_symlinks() {
util::canonicalize(path)
} else {
util::resolve_path(path)
}?;
let path = if config::resolve_symlinks() { util::canonicalize(path) } else { util::resolve_path(path) }?;
let path = util::path_to_str(&path)?;

// Ignore path if it contains unsupported characters, or if it's in
Expand Down
8 changes: 3 additions & 5 deletions src/app/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use std::fs;

impl Run for Import {
fn run(&self) -> Result<()> {
let buffer = fs::read_to_string(&self.path).with_context(|| {
format!("could not open database for importing: {}", &self.path.display())
})?;
let buffer = fs::read_to_string(&self.path)
.with_context(|| format!("could not open database for importing: {}", &self.path.display()))?;

let data_dir = config::data_dir()?;
let mut db = DatabaseFile::new(data_dir);
Expand Down Expand Up @@ -64,8 +63,7 @@ fn from_z<'a>(db: &mut Database<'a>, buffer: &'a str) -> Result<()> {
let mut split = line.rsplitn(3, '|');

let last_accessed = split.next().with_context(|| format!("invalid entry: {}", line))?;
let last_accessed =
last_accessed.parse().with_context(|| format!("invalid epoch: {}", last_accessed))?;
let last_accessed = last_accessed.parse().with_context(|| format!("invalid epoch: {}", last_accessed))?;

let rank = split.next().with_context(|| format!("invalid entry: {}", line))?;
let rank = rank.parse().with_context(|| format!("invalid rank: {}", rank))?;
Expand Down
8 changes: 3 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ pub fn exclude_dirs() -> Result<Vec<Pattern>> {
env::split_paths(&paths)
.map(|path| {
let pattern = path.to_str().context("invalid unicode in _ZO_EXCLUDE_DIRS")?;
Pattern::new(pattern)
.with_context(|| format!("invalid glob in _ZO_EXCLUDE_DIRS: {}", pattern))
Pattern::new(pattern).with_context(|| format!("invalid glob in _ZO_EXCLUDE_DIRS: {}", pattern))
})
.collect()
},
Expand All @@ -61,9 +60,8 @@ pub fn maxage() -> Result<Rank> {
match env::var_os("_ZO_MAXAGE") {
Some(maxage) => {
let maxage = maxage.to_str().context("invalid unicode in _ZO_MAXAGE")?;
let maxage = maxage
.parse::<u64>()
.with_context(|| format!("unable to parse _ZO_MAXAGE as integer: {}", maxage))?;
let maxage =
maxage.parse::<u64>().with_context(|| format!("unable to parse _ZO_MAXAGE as integer: {}", maxage))?;
Ok(maxage as Rank)
}
None => Ok(10000.0),
Expand Down
33 changes: 11 additions & 22 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,18 @@ impl<'file> Database<'file> {
}

let buffer = self.dirs.to_bytes()?;
let mut file = NamedTempFile::new_in(self.data_dir).with_context(|| {
format!("could not create temporary database in: {}", self.data_dir.display())
})?;
let mut file = NamedTempFile::new_in(self.data_dir)
.with_context(|| format!("could not create temporary database in: {}", self.data_dir.display()))?;

// Preallocate enough space on the file, preventing copying later on.
// This optimization may fail on some filesystems, but it is safe to
// ignore it and proceed.
let _ = file.as_file().set_len(buffer.len() as _);
file.write_all(&buffer).with_context(|| {
format!("could not write to temporary database: {}", file.path().display())
})?;
file.write_all(&buffer)
.with_context(|| format!("could not write to temporary database: {}", file.path().display()))?;

let path = db_path(&self.data_dir);
persist(file, &path)
.with_context(|| format!("could not replace database: {}", path.display()))?;
persist(file, &path).with_context(|| format!("could not replace database: {}", path.display()))?;

self.modified = false;
Ok(())
Expand All @@ -51,11 +48,7 @@ impl<'file> Database<'file> {

match self.dirs.iter_mut().find(|dir| dir.path == path) {
None => {
self.dirs.push(Dir {
path: path.to_string().into(),
last_accessed: now,
rank: 1.0,
});
self.dirs.push(Dir { path: path.to_string().into(), last_accessed: now, rank: 1.0 });
}
Some(dir) => {
dir.last_accessed = now;
Expand Down Expand Up @@ -184,23 +177,19 @@ impl DatabaseFile {
match fs::read(&path) {
Ok(buffer) => {
self.buffer = buffer;
let dirs = DirList::from_bytes(&self.buffer).with_context(|| {
format!("could not deserialize database: {}", path.display())
})?;
let dirs = DirList::from_bytes(&self.buffer)
.with_context(|| format!("could not deserialize database: {}", path.display()))?;
Ok(Database { dirs, modified: false, data_dir: &self.data_dir })
}
Err(e) if e.kind() == io::ErrorKind::NotFound => {
// Create data directory, but don't create any file yet.
// The file will be created later by [`Database::save`]
// if any data is modified.
fs::create_dir_all(&self.data_dir).with_context(|| {
format!("unable to create data directory: {}", self.data_dir.display())
})?;
fs::create_dir_all(&self.data_dir)
.with_context(|| format!("unable to create data directory: {}", self.data_dir.display()))?;
Ok(Database { dirs: DirList::new(), modified: false, data_dir: &self.data_dir })
}
Err(e) => {
Err(e).with_context(|| format!("could not read from database: {}", path.display()))
}
Err(e) => Err(e).with_context(|| format!("could not read from database: {}", path.display())),
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/db/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ mod tests {
#[case(&["/foo/", "/bar"], "/foo/bar", false)]
#[case(&["/foo/", "/bar"], "/foo/baz/bar", true)]
fn query(#[case] keywords: &[&str], #[case] path: &str, #[case] is_match: bool) {
let mut db =
Database { dirs: Vec::new().into(), modified: false, data_dir: &PathBuf::new() };
let mut db = Database { dirs: Vec::new().into(), modified: false, data_dir: &PathBuf::new() };
let stream = db.stream(0).with_keywords(keywords);
assert_eq!(is_match, stream.matches_keywords(path));
}
Expand Down
34 changes: 6 additions & 28 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ mod tests {
) {
let opts = Opts { cmd, hook, echo, resolve_symlinks };
let source = Bash(&opts).render().unwrap();

Command::new("bash")
.args(&["--noprofile", "--norc", "-c", &source])
.assert()
.success()
.stdout("")
.stderr("");
Command::new("bash").args(&["--noprofile", "--norc", "-c", &source]).assert().success().stdout("").stderr("");
}

#[rstest]
Expand Down Expand Up @@ -110,19 +104,12 @@ mod tests {

// Filter out lines using edit:*, since those functions
// are only available in the interactive editor.
for line in
Elvish(&opts).render().unwrap().split('\n').filter(|line| !line.contains("edit:"))
{
for line in Elvish(&opts).render().unwrap().split('\n').filter(|line| !line.contains("edit:")) {
source.push_str(line);
source.push('\n');
}

Command::new("elvish")
.args(&["-c", &source, "-norc"])
.assert()
.success()
.stdout("")
.stderr("");
Command::new("elvish").args(&["-c", &source, "-norc"]).assert().success().stdout("").stderr("");
}

#[rstest]
Expand Down Expand Up @@ -184,12 +171,8 @@ mod tests {
let tempdir = tempfile::tempdir().unwrap();
let tempdir = tempdir.path().to_str().unwrap();

let assert = Command::new("nu")
.env("HOME", tempdir)
.args(&["--commands", &source])
.assert()
.success()
.stderr("");
let assert =
Command::new("nu").env("HOME", tempdir).args(&["--commands", &source]).assert().success().stderr("");

if opts.hook != InitHook::Pwd {
assert.stdout("");
Expand Down Expand Up @@ -302,12 +285,7 @@ mod tests {
let mut source = Xonsh(&opts).render().unwrap();
source.push('\n');

Command::new("black")
.args(&["--check", "--diff", "-"])
.write_stdin(source)
.assert()
.success()
.stdout("");
Command::new("black").args(&["--check", "--diff", "-"]).write_stdin(source).assert().success().stdout("");
}

#[rstest]
Expand Down
18 changes: 6 additions & 12 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ use std::path::{Component, Path, PathBuf};
use std::time::SystemTime;

pub fn canonicalize<P: AsRef<Path>>(path: &P) -> Result<PathBuf> {
dunce::canonicalize(path)
.with_context(|| format!("could not resolve path: {}", path.as_ref().display()))
dunce::canonicalize(path).with_context(|| format!("could not resolve path: {}", path.as_ref().display()))
}

pub fn current_dir() -> Result<PathBuf> {
env::current_dir().context("could not get current directory")
}

pub fn current_time() -> Result<Epoch> {
let current_time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.context("system clock set to invalid time")?
.as_secs();
let current_time =
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).context("system clock set to invalid time")?.as_secs();

Ok(current_time)
}
Expand Down Expand Up @@ -51,9 +48,7 @@ pub fn resolve_path<P: AsRef<Path>>(path: &P) -> Result<PathBuf> {

match components.next() {
Some(Component::Prefix(prefix)) => match prefix.kind() {
Prefix::Disk(drive_letter) | Prefix::VerbatimDisk(drive_letter) => {
Some(drive_letter)
}
Prefix::Disk(drive_letter) | Prefix::VerbatimDisk(drive_letter) => Some(drive_letter),
_ => None,
},
_ => None,
Expand Down Expand Up @@ -106,9 +101,8 @@ pub fn resolve_path<P: AsRef<Path>>(path: &P) -> Result<PathBuf> {
components.next();

let current_dir = env::current_dir()?;
let drive_letter = get_drive_letter(&current_dir).with_context(|| {
format!("could not get drive letter: {}", current_dir.display())
})?;
let drive_letter = get_drive_letter(&current_dir)
.with_context(|| format!("could not get drive letter: {}", current_dir.display()))?;
base_path = get_drive_path(drive_letter);
stack.extend(base_path.components());
}
Expand Down
7 changes: 1 addition & 6 deletions tests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ use assert_cmd::Command;
#[test]
fn completions_bash() {
let source = include_str!("../contrib/completions/zoxide.bash");
Command::new("bash")
.args(&["--noprofile", "--norc", "-c", source])
.assert()
.success()
.stdout("")
.stderr("");
Command::new("bash").args(&["--noprofile", "--norc", "-c", source]).assert().success().stdout("").stderr("");
}

// Elvish: the completions file uses editor commands to add completions to the
Expand Down
Loading

0 comments on commit 02029ae

Please sign in to comment.