Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ jobs:
build:
# The release ships Linux and Windows builds too; nothing else compiles
# them, so a change that breaks either shows up here, on the pull request,
# rather than on a release tag. Build only: the tests run on macOS above.
# rather than on a release tag. Linux builds only; Windows runs the tests
# as well, since its paths (drive letters, `\`, no `$HOME`) are where the
# macOS run cannot catch a mistake.
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -61,3 +63,7 @@ jobs:

- name: Build
run: cargo build --locked

- name: Run tests
if: runner.os == 'Windows'
run: cargo test --locked
109 changes: 101 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ image = { version = "0.25", default-features = false, features = ["png", "jpeg"]

[dev-dependencies]
rust_xlsxwriter = "0.99"
# Same release tag as the build dependency (see above); the extra feature only
# joins the test build, where `gpui_kit::test` and `#[gpui_kit::test]` live.
gpui-kit = { git = "https://github.com/longbridge/gpui-kit", rev = "0c830f4d257e69fdd17200650533ab4ca9a40cc0", features = ["test-support"] }

# `gpui-shell` scripts run on the QuickJS fork the toolkit pins; the crates.io
# `rquickjs` would be a second, incompatible copy of the same crate.
Expand Down
38 changes: 32 additions & 6 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,18 @@ impl DatabaseTarget {

/// `$HOME`, read once. `file_label` runs on every frame of both the title
/// bar and the status bar, so it should not go back to the environment each
/// time.
/// time. Windows does not set `HOME` outside a Unix-style shell; its home is
/// `USERPROFILE`.
fn home() -> Option<&'static str> {
static HOME: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
HOME.get_or_init(|| std::env::var("HOME").ok()).as_deref()
HOME.get_or_init(|| {
std::env::var("HOME")
.or_else(|_| std::env::var("USERPROFILE"))
.ok()
.map(|home| home.trim_end_matches(std::path::is_separator).to_string())
.filter(|home| !home.is_empty())
})
.as_deref()
}

/// Display `$HOME` as `~`.
Expand All @@ -93,16 +101,19 @@ pub(crate) fn compact_home(path: &str) -> String {
/// home of `/Users/al` leaves `/Users/alice/x` alone.
fn compact_home_under(path: &str, home: &str) -> String {
match path.strip_prefix(home) {
Some(rest) if rest.is_empty() || rest.starts_with('/') => format!("~{rest}"),
Some(rest) if rest.is_empty() || rest.starts_with(std::path::is_separator) => {
format!("~{rest}")
}
_ => path.to_string(),
}
}

/// Replace a `~/` prefix with `$HOME` expanded.
/// Replace a `~/` prefix (or `~\` on Windows) with `$HOME` expanded.
pub fn expand_tilde(path: &str) -> String {
if let Some(rest) = path.strip_prefix("~/") {
let mut chars = path.chars();
if chars.next() == Some('~') && chars.next().is_some_and(std::path::is_separator) {
if let Some(home) = home() {
return format!("{home}/{rest}");
return format!("{home}{}", &path[1..]);
}
}
path.to_string()
Expand Down Expand Up @@ -486,6 +497,21 @@ mod tests {
assert_eq!(compact_home_under("/Users/alice/x", "/Users/al"), "/Users/alice/x");
}

#[cfg(windows)]
#[test]
fn windows_home_is_compacted_at_a_backslash() {
assert_eq!(compact_home_under(r"C:\Users\al\x.csv", r"C:\Users\al"), r"~\x.csv");
assert_eq!(compact_home_under(r"C:\Users\alice\x", r"C:\Users\al"), r"C:\Users\alice\x");
}

#[cfg(windows)]
#[test]
fn windows_tilde_expansion_takes_a_backslash() {
let expanded = expand_tilde(r"~\data\x.duckdb");
assert!(!expanded.starts_with('~'));
assert!(expanded.ends_with(r"\data\x.duckdb"));
}

#[test]
fn tilde_expansion() {
let expanded = expand_tilde("~/data/x.duckdb");
Expand Down
Loading