Skip to content

Commit

Permalink
feat: impl list command, custom log output (#68)
Browse files Browse the repository at this point in the history
* feat: impl custom logging output format

* feat: impl list command

* style: run cargo fmt

* doc: update book with list command
  • Loading branch information
Shadow53 authored Dec 27, 2021
1 parent f74c3d3 commit cdb863c
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 122 deletions.
83 changes: 37 additions & 46 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM rust:alpine

ENV RUSTFLAGS="-Zinstrument-coverage"
ENV LLVM_PROFILE_FILE="profraw/hoard-python-test-%p-%m.profraw"
ENV CI=true GITHUB_ACTIONS=true HOARD_LOG=debug
ENV CI=true GITHUB_ACTIONS=true HOARD_LOG=trace
WORKDIR /hoard-tests

RUN apk add python3 tree py3-yaml py3-toml
Expand Down
2 changes: 2 additions & 0 deletions book/src/cli/flags-subcommands.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Flags can be used with any subcommand and must be specified *before* any subcomm
- Back up the specified hoard(s). If no `name` is specified, all hoards are backed up.
- Restore: `hoard [flags...] restore [name] [name] [...]`
- Restore the specified hoard(s). If no `name` is specified, all hoards are restored.
- List Hoards: `hoard list`
- List all configured hoards by name
- Validate: `hoard [flags...] validate`
- Attempt to parse the default configuration file (or the one provided via `--config-file`)
Exits with code `0` if the config is valid.
Expand Down
67 changes: 33 additions & 34 deletions ci-tests/tests/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import subprocess
import sys
from pathlib import Path
from testers.ignore_filter import IgnoreFilterTester
from testers.last_paths import LastPathsTester
from testers.operations import OperationCheckerTester
from testers.cleanup import LogCleanupTester
from testers.hoard_tester import HoardFile, Environment
from testers.correct_errors import CorrectErrorsTester
from testers.hoard_list import ListHoardsTester
from testers.hoard_tester import HoardFile, Environment
from testers.ignore_filter import IgnoreFilterTester
from testers.last_paths import LastPathsTester
from testers.no_config_dir import MissingConfigDirTester
from testers.operations import OperationCheckerTester
from testers.yaml_support import YAMLSupportTester


Expand Down Expand Up @@ -43,43 +44,41 @@ def print_checksums():
print(f"{path}: {hashlib.md5(file.read()).hexdigest()}")


TEST_MAPPING = {
"cleanup": ("cleanup", LogCleanupTester),
"errors": ("expected errors", CorrectErrorsTester),
"ignore": ("ignore filter", IgnoreFilterTester),
"last_paths": ("last paths", LastPathsTester),
"list_hoards": ("list command", ListHoardsTester),
"missing_config": ("missing config dir", MissingConfigDirTester),
"operation": ("operation", OperationCheckerTester),
"yaml": ("YAML compat", YAMLSupportTester),
}


if __name__ == "__main__":
if len(sys.argv) == 1:
raise RuntimeError("One argument - the test - is required")
try:
if sys.argv[1] == "last_paths":
print("Running last_paths test")
LastPathsTester().run_test()
elif sys.argv[1] == "operation":
print("Running operation test")
OperationCheckerTester().run_test()
elif sys.argv[1] == "ignore":
print("Running ignore filter test")
IgnoreFilterTester().run_test()
elif sys.argv[1] == "cleanup":
print("Running cleanup test")
LogCleanupTester().run_test()
elif sys.argv[1] == "errors":
print("Running errors test")
CorrectErrorsTester().run_test()
elif sys.argv[1] == "missing_config":
print("Running missing config dir test")
MissingConfigDirTester().run_test()
elif sys.argv[1] == "yaml":
print("Running YAML compat tests")
YAMLSupportTester().run_test()
elif sys.argv[1] == "all":
test_arg = sys.argv[1]
if test_arg == "all":
print("Running all tests")
YAMLSupportTester().run_test()
MissingConfigDirTester().run_test()
CorrectErrorsTester().run_test()
LastPathsTester().run_test()
IgnoreFilterTester().run_test()
OperationCheckerTester().run_test()
LogCleanupTester().run_test()
successful = []
for desc, cls in TEST_MAPPING.values():
print(f"=== Running {desc} test ===")
cls().run_test()
successful.append(desc)
elif test_arg in TEST_MAPPING:
desc, cls = TEST_MAPPING[test_arg]
print(f"Running {desc} test")
cls().run_test()
else:
raise RuntimeError(f"Invalid argument {sys.argv[1]}")
raise RuntimeError(f"Invalid argument {test_arg}")
except Exception:
if desc:
print(f"=== Error while running {desc} test ===")
if successful and len(successful) > 0:
print(f"=== Successful tests: {', '.join(successful)} ===")
data_dir = LastPathsTester.data_dir_path()
print("\n### Hoards:")
sys.stdout.flush()
Expand Down
16 changes: 16 additions & 0 deletions ci-tests/tests/testers/hoard_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from .hoard_tester import HoardTester


class ListHoardsTester(HoardTester):
def __init__(self):
super().__init__()
self.reset()

def run_test(self):
expected = b"anon_dir\nanon_file\nnamed\n"
output = self.run_hoard("list", capture_output=True)
assert expected in output.stdout

self.env["HOARD_LOG"] = "info"
output = self.run_hoard("list", capture_output=True)
assert output.stdout == expected
6 changes: 5 additions & 1 deletion ci-tests/tests/testers/hoard_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,13 @@ def reset(self, config_file="config.toml"):

for env in list(Environment):
for item in list(HoardFile):
path = home.joinpath(f"{env}_{item}")
if item is HoardFile.AnonDir or item is HoardFile.NamedDir1 or item is HoardFile.NamedDir2:
try:
shutil.rmtree(path)
except FileNotFoundError:
pass
continue
path = home.joinpath(f"{env}_{item}")
self.generate_file(path)
os.makedirs(config_parent, exist_ok=True)
config_file_src = Path.cwd().joinpath("ci-tests", config_file)
Expand Down
10 changes: 9 additions & 1 deletion ci-tests/tests/testers/last_paths.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .hoard_tester import HoardTester
from pathlib import Path
import subprocess
from .hoard_tester import HoardTester, Environment, HoardFile, Hoard


class LastPathsTester(HoardTester):
Expand Down Expand Up @@ -27,8 +28,15 @@ def run_test(self):
# Doing it again with "first" should still succeed
self.env = {"USE_ENV": "1"}
self.run_hoard("backup")

home = Path.home()
ignored = [
home.joinpath(),
]

# Make sure the files are consistent with backing up "first"
self.assert_first_tree()

# Doing it with "second" but forced should succeed
self.env = {"USE_ENV": "2"}
self.force = True
Expand Down
4 changes: 3 additions & 1 deletion src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub enum Command {
/// The name(s) of the hoard(s) to restore. Will restore all hoards if empty.
hoards: Vec<String>,
},
/// List configured hoards
List,
}

impl Default for Command {
Expand All @@ -41,7 +43,7 @@ mod tests {
use super::*;

#[test]
fn default_command_is_help() {
fn default_command_is_validate() {
// The default command is validate if one is not given
assert_eq!(Command::Validate, Command::default());
}
Expand Down
Loading

0 comments on commit cdb863c

Please sign in to comment.