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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"command": "uv run python -m pytest tests/e2e/declared_table_name_test.py tests/e2e/query_contract_test.py -x -q",
"ran_at": 1795054070,
"exit_code": 0,
"commit": "48d903c7b99300cbc90c02f9771c6273ba76aa4e",
"branch": "bot/1770-windows-dotdot-glob-1118"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**Fixed** On Windows, a config `[[table]] glob` starting with `../` now anchors above the config's directory when the config path is a verbatim (`\\?\`) path, as the CLI's `-c` produces; it used to match nothing.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
### Rust core: `../` config globs anchor correctly under a verbatim config path

**Summary**

On Windows, a config glob such as `../data/*.json` loaded from a verbatim (`\\?\`) config path now anchors at the config directory's parent, as it already did on other platforms and for non-verbatim paths. A bug fix; nothing breaks.

**Required changes**

_None._

**Deprecations removed**

_None._

**Behavior changes without code changes**

On Windows, a `../` config glob under a verbatim config path that used to match nothing now matches the files it names.

**Verification**

```bash
cargo test -p dirsql --features cli --test cli_config_anchor
# test result: ok.
```
26 changes: 24 additions & 2 deletions packages/rust/src/path_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,17 +313,27 @@ fn absolute_target<S: Syntax>(
home: Option<&Path>,
) -> Option<Option<Utf8PathBuf<S>>> {
if let Some(rest) = S::HOME_PREFIXES.iter().find_map(|p| name.strip_prefix(p)) {
return Some(home.map(|h| typed::<S>(h).join(rest)));
return Some(home.map(|h| join_written(typed::<S>(h), rest)));
}
if name.starts_with("../") {
return Some(Some(typed::<S>(index_root).join(name)));
return Some(Some(join_written(typed::<S>(index_root), name)));
}
if S::is_rooted(name) {
return Some(Some(Utf8PathBuf::from(name)));
}
None
}

/// `base` with the written `rest` appended one component at a time. Joined
/// whole onto a verbatim (`\\?\`) base, `rest` would be one component with
/// its `/` kept literal.
fn join_written<S: Syntax>(mut base: Utf8PathBuf<S>, rest: &str) -> Utf8PathBuf<S> {
for part in rest.split(S::SEPARATORS).filter(|p| !p.is_empty()) {
base.push(part);
}
base
}

/// Fold `.` and `..` out of `path` lexically. Purely textual: a `..` is not
/// resolved through a symlink.
fn normalize<S: Syntax>(path: &Utf8Path<S>) -> Utf8PathBuf<S> {
Expand Down Expand Up @@ -1123,6 +1133,18 @@ mod tests {
);
}

#[test]
fn a_parent_relative_config_glob_climbs_out_of_a_verbatim_config_directory() {
assert_eq!(
config_anchor_as::<Utf8WindowsEncoding>(
"../data/*.json",
Path::new(r"\\?\C:\work\cfg"),
None
),
Some((PathBuf::from(r"\\?\C:\work\data"), "*.json".to_string()))
);
}

#[test]
fn a_drive_letter_path_folds_a_parent_component_as_windows_does() {
let t = table(r"C:\var\x\..\log\*.log", &nothing_is_a_dir);
Expand Down
47 changes: 47 additions & 0 deletions packages/rust/tests/cli_config_anchor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! A config glob passed with `-c` anchors at the config's directory, `../`
//! included.
//!
//! Gated behind `--features cli` like the sibling CLI e2e tests.
#![cfg(feature = "cli")]

use std::fs;

use assert_cmd::prelude::*;
use tempfile::TempDir;

const HOOK: &str = r#"on-file = '''sh -c 'for p; do p=$(printf %s "$p" | tr "\\\\" /); printf "{\"basename\":\"%s\"}\n" "${p##*/}"; done' sh'''"#;

#[test]
fn a_parent_relative_config_glob_matches_files_above_the_config() {
let dir = TempDir::new().unwrap();
let cfg_dir = dir.path().join("cfg");
fs::create_dir(&cfg_dir).unwrap();
fs::create_dir(dir.path().join("data")).unwrap();
fs::write(dir.path().join("data/a.json"), "{}").unwrap();
let cfg = cfg_dir.join(".dirsql.toml");
fs::write(
&cfg,
format!(
"[[table]]\nname = \"docs\"\nddl = \"CREATE TABLE docs (basename TEXT)\"\nglob = '../data/*.json'\n{HOOK}\n"
),
)
.unwrap();

let out = std::process::Command::cargo_bin("dirsql")
.expect("`dirsql` binary must be built by `cargo test` with --features cli")
.arg("query")
.arg("SELECT basename FROM docs")
.arg("-c")
.arg(&cfg)
.current_dir(&cfg_dir)
.output()
.unwrap();

let stdout = String::from_utf8_lossy(&out.stdout);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(out.status.success(), "stderr: {stderr}");
assert!(
stdout.contains("a.json"),
"stdout: {stdout}\nstderr: {stderr}"
);
}
31 changes: 31 additions & 0 deletions packages/rust/tests/cli_missing_anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,34 @@ fn a_config_table_whose_directory_is_missing_fails_the_query() {
"{stderr}"
);
}

#[test]
fn a_config_table_above_the_config_whose_directory_is_missing_fails_the_query() {
let dir = TempDir::new().unwrap();
let cfg_dir = dir.path().join("cfg");
fs::create_dir(&cfg_dir).unwrap();
let cfg = cfg_dir.join(".dirsql.toml");
fs::write(
&cfg,
"[[table]]\nname = \"later\"\nddl = \"CREATE TABLE later (x TEXT)\"\nglob = '../absent/*.json'\non-file = \"true\"\n",
)
.unwrap();

let out = std::process::Command::cargo_bin("dirsql")
.expect("`dirsql` binary must be built by `cargo test` with --features cli")
.arg("query")
.arg("SELECT COUNT(*) AS n FROM later")
.arg("-c")
.arg(&cfg)
.current_dir(&cfg_dir)
.output()
.unwrap();

let stderr = String::from_utf8_lossy(&out.stderr);
assert!(!out.status.success(), "exited zero; stderr: {stderr}");
assert!(
stderr.contains("table 'later': its directory ")
&& stderr.contains("absent does not exist"),
"{stderr}"
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"command": "pnpm test:e2e",
"ran_at": 1795054143,
"exit_code": 0,
"commit": "4d39a95ae69bddf7dae5c8998d02553a304931e5",
"branch": "bot/1770-windows-dotdot-glob-1118"
}
Loading