Skip to content
Open
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
5 changes: 5 additions & 0 deletions crates/graphicle/src/muse_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,12 @@ pub fn session_pack(directory: &Path, project: &str, tokens: u64) -> Option<Stri
/// laid out relevant-first, the most connected files first, the order in
/// which a session start that has to cut it keeps them ([`session_start`]).
/// One build at a time per pack: a second one finds the lock and leaves.
/// In read-only mode ([`crate::db::read_only`]) nothing is built or written:
/// the pack is refused.
pub fn build_pack(store: &crate::store::Store, project: &str, tokens: u64) -> Result<PathBuf> {
if crate::db::read_only() {
return Err(Error::bad(crate::db::READ_ONLY_REFUSAL));
}
let session = crate::local::session(store, false)?;
let path = pack_path(&store.directory, project, tokens);
let folder = path
Expand Down
96 changes: 96 additions & 0 deletions crates/graphicle/tests/it/read_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,102 @@ fn a_read_only_pack_writes_no_query_file() {
assert!(files(&data) == before, "the data folder changed");
}

/// The Muse Code session map build (`graphicle hook muse pack`, which a
/// session start runs detached) writes its pack into the data folder; in
/// read-only mode, by flag or variable, it writes nothing there.
#[test]
fn a_read_only_muse_pack_build_writes_nothing() {
if graphicle_runtime::db::postgres_configured() {
eprintln!(
"skipped: read-only mode opens SQLite data folders; GRAPHICLE_DATABASE_URL is set"
);
return;
}
let dir = TempDir::new().unwrap();
let home = dir.path().join("home");
let code = dir.path().join("code");
let writable_data = dir.path().join("writable-data");
let read_only_data = dir.path().join("read-only-data");
std::fs::create_dir_all(&home).unwrap();
std::fs::create_dir_all(&code).unwrap();
std::fs::write(
code.join("prices.py"),
"def price_with_tax(price):\n return round(price * 1.2, 2)\n",
)
.unwrap();
let sync = |data: &Path| {
let mut sync = graphicle(&home, data, &code);
sync.args(["sync", ".", "--json"]);
let synced: serde_json::Value = serde_json::from_str(&ok(sync)).unwrap();
let project = synced["repository_id"].as_str().unwrap().to_string();
assert!(!project.is_empty(), "the sync returned an empty project id");
project
};
let writable_project = sync(&writable_data);
let read_only_project = sync(&read_only_data);

let mut writable = graphicle(&home, &writable_data, &code);
writable.args(["hook", "muse", "pack", "--project", &writable_project]);
let built = hook(writable, "");
assert!(
built.status.success(),
"{}",
String::from_utf8_lossy(&built.stderr)
);
let pack_folder = writable_data.join(graphicle_runtime::muse_code::PACK_FOLDER);
let packs: Vec<_> = std::fs::read_dir(&pack_folder)
.unwrap()
.map(|entry| entry.unwrap().path())
.filter(|path| path.extension().is_some_and(|extension| extension == "md"))
.collect();
assert_eq!(packs.len(), 1, "expected one Muse pack in {pack_folder:?}");
let pack = std::fs::read_to_string(&packs[0]).unwrap();
assert!(pack.contains("price_with_tax"), "{pack}");

let before = files(&read_only_data);
let mut pack = graphicle(&home, &read_only_data, &code);
pack.args(["--read-only", "pack", "where is tax added to prices?"]);
assert!(ok(pack).contains("price_with_tax"));
let mut read_only = graphicle(&home, &read_only_data, &code);
read_only.args([
"--read-only",
"hook",
"muse",
"pack",
"--project",
&read_only_project,
]);
let refused = hook(read_only, "");
assert!(
refused.status.success(),
"{}",
String::from_utf8_lossy(&refused.stderr)
);
assert!(
files(&read_only_data) == before,
"the muse pack build changed the read-only data folder"
);

let mut read_only = graphicle(&home, &read_only_data, &code);
read_only.env("GRAPHICLE_READ_ONLY", "1").args([
"hook",
"muse",
"pack",
"--project",
&read_only_project,
]);
let refused = hook(read_only, "");
assert!(
refused.status.success(),
"{}",
String::from_utf8_lossy(&refused.stderr)
);
assert!(
files(&read_only_data) == before,
"the muse pack build changed the read-only data folder"
);
}

/// A backup in the data folder is refused before it writes anything, while a
/// backup explicitly written outside the data folder remains allowed.
#[test]
Expand Down
7 changes: 4 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ not need `graphicle init`.
`--read-only` on any command, or `GRAPHICLE_READ_ONLY=1`, opens the data
folder without changing it: reads such as `graphicle context`,
`graphicle call context` and `graphicle pack` are answered; the store, its
query files and the count of tokens served are not written; a folder's
changes are not synced before an answer, which says so; and commands that
write, such as `sync`, `login` and `config set`, are refused with a message.
query files, the count of tokens served and Muse Code's session map are not
written; a folder's changes are not synced before an answer, which says so;
and commands that write, such as `sync`, `login` and `config set`, are
refused with a message.
Use it to serve a store from read-only media, or to query the only copy of
one. A project whose saved query files were written by another version is
read from the store instead, more slowly.
Expand Down
Loading