Skip to content

Commit

Permalink
test(Jupyter kernels): Add tests for getting Jupyter directories
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Oct 25, 2023
1 parent fa18a3b commit b2e139c
Showing 1 changed file with 65 additions and 29 deletions.
94 changes: 65 additions & 29 deletions rust/kernel-jupyter/src/dirs.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
use std::{env, path::PathBuf, process::Command};

/// Run the `jupyter` command to get lists of directories
///
/// This can be necessary if Jupyter has been installed using something
/// like `mamba` (and maybe Nix?) in which case the rules described in
/// the Jupyter documentation and implemented below may not apply.
///
/// An optimization could be to store `static` results and throttle
/// calls to `jupyter`.
fn jupyter_paths() -> (Vec<PathBuf>, Vec<PathBuf>) {
let mut data = Vec::new();
let mut runtime = Vec::new();
if let Ok(output) = Command::new("jupyter").arg("--path").output() {
if let Ok(stdout) = std::str::from_utf8(&output.stdout) {
let mut group = "";
for line in stdout.lines() {
let line = line.trim();
if line.ends_with(':') {
group = line;
} else if group == "data:" {
data.push(PathBuf::from(line));
} else if group == "runtime:" {
runtime.push(PathBuf::from(line));
}
}
}
}
(data, runtime)
}

/// Get *the* Jupyter data directory.
///
/// See https://jupyter.readthedocs.io/en/latest/use/jupyter-directories.html.
Expand Down Expand Up @@ -113,6 +84,38 @@ pub fn runtime_dirs() -> Vec<PathBuf> {
dirs
}

/// Run the `jupyter` command to get lists of data and runtime directories
///
/// This can be necessary if Jupyter has been installed using something
/// like `mamba` (and maybe Nix?) in which case the rules described in
/// the Jupyter documentation and implemented below may not apply.
///
/// An optimization could be to store `static` results and throttle
/// calls to `jupyter`.
fn jupyter_paths() -> (Vec<PathBuf>, Vec<PathBuf>) {
let mut data = Vec::new();
let mut runtime = Vec::new();
if let Ok(output) = Command::new("python")
.args(["-m", "jupyter", "--path"])
.output()
{
if let Ok(stdout) = std::str::from_utf8(&output.stdout) {
let mut group = "";
for line in stdout.lines() {
let line = line.trim();
if line.ends_with(':') {
group = line;
} else if group == "data:" {
data.push(PathBuf::from(line));
} else if group == "runtime:" {
runtime.push(PathBuf::from(line));
}
}
}
}
(data, runtime)
}

/// Add a path if it is missing from a set of paths
fn push_missing(paths: &mut Vec<PathBuf>, path: PathBuf) {
if !paths.contains(&path) {
Expand All @@ -128,3 +131,36 @@ fn append_missing(paths: &mut Vec<PathBuf>, others: Vec<PathBuf>) {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[cfg(target_os = "linux")]
#[test]
fn all() {
use kernel::common::itertools::Itertools;

assert!(data_dir()
.to_string_lossy()
.ends_with(".local/share/jupyter"));

let dirs = data_dirs()
.iter()
.map(|dir| dir.to_string_lossy().to_string())
.collect_vec();
assert!(dirs[0].ends_with("share/jupyter"));

let dirs = kernel_dirs()
.iter()
.map(|dir| dir.to_string_lossy().to_string())
.collect_vec();
assert!(dirs[0].ends_with("share/jupyter/kernels"));

let dirs = runtime_dirs()
.iter()
.map(|dir| dir.to_string_lossy().to_string())
.collect_vec();
assert!(dirs[0].ends_with("share/jupyter/runtime"));
}
}

0 comments on commit b2e139c

Please sign in to comment.