Add test for device metadata file creation and fixes until test passed.

This commit is contained in:
Sebastian Lenzlinger
2024-05-08 02:36:07 +02:00
parent a21312ee61
commit 266a669e5e
3 changed files with 61 additions and 13 deletions

View File

@@ -19,26 +19,36 @@ def setup_init_device_root_parser(subparsers):
def handle_add(args):
logger.info(f"Add device handler called with args {args}")
args.root_dir.mkdir(parents=True, exist_ok=True) # else metadata.save_to_file will fail TODO: unclear what to assume
if args.guided:
logger.debug("Guided setup")
logger.debug("begin guided setup")
metadata = guided_setup(args.root_dir)
logger.debug("guided setup complete")
else:
logger.debug("Setup through passed args: setup")
device_name = args.name
args.root_dir.mkdir(parents=True, exist_ok=True)
metadata = DeviceMetadata(device_name, args.root_dir)
if not args.name:
logger.error("No device name specified with unguided setup.")
return ReturnCodes.ERROR
metadata = DeviceMetadata(args.name, args.root_dir)
file_path = args.root_dir / DEVICE_METADATA_FILE
response = input(f"Confirm device metadata: {metadata.to_json()} [y/N]")
if response.lower() not in definitions.AFFIRMATIVE_USER_RESPONSE.add(""):
configure_metadata()
assert False, "TODO implement dynamic setup"
args.root_dir.mkdir(parents=True, exist_ok=True) # else metadata.save_to_file will fail TODO: unclear
if metadata.save_to_json(file_path) == ReturnCodes.FILE_ALREADY_EXISTS:
print("Directory already contains a device metadata file. Aborting operation.")
if file_path.exists():
print("Directory already contains a metadata file. Aborting.")
return ReturnCodes.ABORTED
assert Path(file_path).exists(), f"{file_path} does not exist"
serialized_metadata = metadata.to_json()
response = input(f"Confirm device metadata: {serialized_metadata} [y/N]")
logger.debug(f"response: {response}")
if response not in definitions.AFFIRMATIVE_USER_RESPONSE:
print("Adding device aborted by user.")
return ReturnCodes.ABORTED
logger.debug(f"Device metadata file {file_path}")
if metadata.save_to_json(file_path) == ReturnCodes.FILE_ALREADY_EXISTS:
logger.error("File exists after checking, which should not happen.")
return ReturnCodes.ABORTED
print("Device metadata successfully created.")
return ReturnCodes.SUCCESS