FFFF Fail on an object jetdb prop does not find, as the other show commands do by dominion525 · Pull Request #174 · dominion525/jetdb · GitHub
[go: up one dir, main page]

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
50 changes: 43 additions & 7 deletions crates/jetdb-cli/src/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,48 @@ pub fn cmd_prop(args: PropArgs, password: Option<&str>) -> ExitCode {
fn run_prop(args: &PropArgs, password: Option<&str>) -> Result<(), String> {
let mut reader =
PageReader::open_with_password(&args.file, password).map_err(|e| e.to_string())?;
let object_type = object_type_of(&mut reader, args)?;
let catalog = read_catalog(&mut reader).map_err(|e| e.to_string())?;
let object_type = object_type_of(&catalog, args)?;
// Without a type, the object may be of a type the catalog leaves out:
// read it by its name alone.
let props = match object_type {
Some(t) => read_object_properties_of_type(&mut reader, &args.object_name, t),
None => read_object_properties(&mut reader, &args.object_name),
}
.map_err(|e| e.to_string())?;
// An object with properties is there even when the catalog leaves it
// out; one without them is there when the catalog has it.
if props.maps.is_empty() && !in_catalog(&catalog, &args.object_name, object_type) {
return Err(not_found(args));
}
print_properties(&props, object_type);
Ok(())
}

/// The type of the object of `prop`: the one `--type` names, or the only
/// one of that name in the catalog, or `None` when there is none.
fn object_type_of(reader: &mut PageReader, args: &PropArgs) -> Result<Option<ObjectType>, String> {
/// one of that name in `catalog`, or `None` when there is none.
fn object_type_of(catalog: &[CatalogEntry], args: &PropArgs) -> Result<Option<ObjectType>, String> {
match &args.object_type {
Some(name) => Ok(Some(object_type_named(name))),
None => {
let catalog = read_catalog(reader).map_err(|e| e.to_string())?;
only_type_of(&catalog, &args.object_name)
}
None => only_type_of(catalog, &args.object_name),
}
}

/// Whether `catalog` has an object named `name`, of `object_type` when it
/// is given.
fn in_catalog(catalog: &[CatalogEntry], name: &str, object_type: Option<ObjectType>) -> bool {
catalog
.iter()
.any(|e| e.name == name && object_type.is_none_or(|t| e.object_type == t))
}

/// The error of `prop` for an object it does not find, naming the type
/// `--type` names.
fn not_found(args: &PropArgs) -> String {
let kind = args.object_type.as_deref().unwrap_or("object");
format!("{kind} not found: {}", args.object_name)
}

/// The object type of a name `--type` takes.
fn object_type_named(name: &str) -> ObjectType {
ObjectType::ALL
Expand Down Expand Up @@ -209,6 +227,24 @@ mod tests {
);
}

#[test]
fn in_catalog_of_a_type() {
let entry = CatalogEntry {
name: "Customers".to_string(),
object_type: ObjectType::Table,
table_page: 0,
flags: 0,
lv_prop: None,
lv_extra: None,
};
let catalog = [entry];
assert!(in_catalog(&catalog, "Customers", None));
assert!(in_catalog(&catalog, "Customers", Some(ObjectType::Table)));
assert!(!in_catalog(&catalog, "Customers", Some(ObjectType::Query)));
assert!(!in_catalog(&catalog, "CUSTOMERS", None));
assert!(!in_catalog(&catalog, "Orders", None));
}

#[test]
fn format_value_bool_true() {
assert_eq!(format_value(&Value::Bool(true)), "yes");
Expand Down
72 changes: 66 additions & 6 deletions crates/jetdb-cli/tests/cli_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn prop_table1() {
}

// ---------------------------------------------------------------------------
// Nonexistent table: empty output
// Nonexistent object: error
// ---------------------------------------------------------------------------

#[test]
Expand All @@ -74,16 +74,76 @@ fn prop_nonexistent_table() {
.output()
.expect("failed to run jetdb");
assert!(
output.status.success(),
"should succeed with empty output for nonexistent object"
!output.status.success(),
"should fail for nonexistent object"
);
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(output.stdout.is_empty());
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stdout.trim().is_empty(),
"should produce empty output for nonexistent object, got:\n{stdout}"
stderr.contains("jetdb: object not found: NoSuchTable_XYZ"),
"got: {stderr}"
);
}

#[test]
fn prop_name_of_another_case_is_not_found() {
let path = skip_if_missing!("V2010/macroGeneratedTestV2010.accdb");
let output = jetdb_bin()
.args(["prop", path.to_str().unwrap(), "TBLITEMS"])
.output()
.expect("failed to run jetdb");
assert!(!output.status.success(), "should fail for TBLITEMS");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("jetdb: object not found: TBLITEMS"),
"got: {stderr}"
);
}

#[test]
fn prop_object_of_another_type_is_not_found() {
// frmEmbedded is a form, not a query.
let path = skip_if_missing!("V2010/macroGeneratedTestV2010.accdb");
let output = jetdb_bin()
.args([
"prop",
"--type",
"query",
path.to_str().unwrap(),
"frmEmbedded",
])
.output()
.expect("failed to run jetdb");
assert!(!output.status.success(), "should fail for a query");
assert!(output.stdout.is_empty());
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("jetdb: query not found: frmEmbedded"),
"got: {stderr}"
);
}

// ---------------------------------------------------------------------------
// Object without properties: empty output
// ---------------------------------------------------------------------------

#[test]
fn prop_object_without_properties() {
// MSysObjects has no properties.
let path = skip_if_missing!("V2003/testV2003.mdb");
let output = jetdb_bin()
.args(["prop", path.to_str().unwrap(), "MSysObjects"])
.output()
.expect("failed to run jetdb");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.is_empty(), "got:\n{stdout}");
}

// ---------------------------------------------------------------------------
// Objects of several types sharing a name: --type chooses one
// ---------------------------------------------------------------------------
Expand Down
Loading
0