From 02c53afea1ec6c6ae5060e01f908497a01f03981 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 15 Oct 2020 09:41:20 +0300 Subject: feat: create new draft, publish it --- subplot/jt.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ subplot/jt.yaml | 27 ++++++++++++++++++ 2 files changed, 113 insertions(+) (limited to 'subplot') diff --git a/subplot/jt.py b/subplot/jt.py index 1b46b0b..b9a7122 100644 --- a/subplot/jt.py +++ b/subplot/jt.py @@ -17,6 +17,18 @@ def run_jt_is_journal(ctx, dirname=None): runcmd_run(ctx, [_binary("jt"), "is-journal", dirname]) +def run_jt_new(ctx, title=None, dirname=None): + runcmd_run = globals()["runcmd_run"] + runcmd_run( + ctx, [_binary("jt"), "new", title, "--dirname", dirname, "--editor=none"] + ) + + +def run_jt_finish(ctx, dirname=None): + runcmd_run = globals()["runcmd_run"] + runcmd_run(ctx, [_binary("jt"), "finish", "--dirname", dirname]) + + def _binary(name): srcdir = globals()["srcdir"] return os.path.join(srcdir, "target", "debug", "jt2") @@ -32,3 +44,77 @@ def output_contains(ctx, pattern=None): assert_eq = globals()["assert_eq"] logging.debug("checking if %r contains", ctx["stdout"], pattern) assert_eq(pattern in ctx["stdout"], True) + + +def journal_has_no_drafts(ctx, dirname=None): + assert_eq = globals()["assert_eq"] + logging.debug(f"checking {dirname} has no drafts") + drafts = os.path.join(dirname, "drafts") + assert_eq(_find_files(drafts), []) + + +def journal_has_one_draft(ctx, dirname=None): + assert_eq = globals()["assert_eq"] + logging.debug(f"checking {dirname} has one draft") + drafts = os.path.join(dirname, "drafts") + assert_eq(len(_find_files(drafts)), 1) + + +def journal_has_no_entries(ctx, dirname=None): + assert_eq = globals()["assert_eq"] + logging.debug(f"checking {dirname} has no entries") + entries = os.path.join(dirname, "entries") + assert_eq(_find_files(entries), []) + + +def journal_has_one_entry(ctx, dirname=None, variable=None): + assert_eq = globals()["assert_eq"] + logging.debug( + f"checking {dirname} has one entry, whose filename is remembered as {variable}" + ) + entries = os.path.join(dirname, "entries") + files = _find_files(entries) + assert_eq(len(files), 1) + variables = ctx.get("variables", {}) + variables[variable] = files[0] + ctx["variables"] = variables + + +def _find_files(root): + if not os.path.exists(root): + return [] + + files = [] + for dirname, _, basenames in os.walk(root): + for basename in basenames: + files.append(os.path.join(dirname, basename)) + return files + + +def draft_contains_string(ctx, dirname=None, draftno=None, pattern=None): + logging.debug(f"checking draft {draftno} in {dirname} has contains {pattern!r}") + draft = os.path.join(dirname, "drafts", f"{draftno}.md") + with open(draft) as f: + data = f.read() + logging.debug(f"draft content: {data!r}") + assert pattern in data + + +def edit_draft(ctx, dirname=None, draftno=None, text=None): + logging.debug(f"editing draft {draftno} in {dirname} to also contain {text!r}") + draft = os.path.join(dirname, "drafts", f"{draftno}.md") + with open(draft, "a") as f: + f.write(text) + + +def file_contains(ctx, variable=None, pattern=None): + logging.debug(f"checking {variable} contains {pattern!r}") + + variables = ctx.get("variables", {}) + logging.debug(f"variables: {variables!r}") + + filename = variables[variable] + with open(filename) as f: + data = f.read() + logging.debug(f"file content: {data!r}") + assert pattern in data diff --git a/subplot/jt.yaml b/subplot/jt.yaml index 77b8c82..5ed6ed3 100644 --- a/subplot/jt.yaml +++ b/subplot/jt.yaml @@ -8,9 +8,36 @@ - when: I invoke jt is-journal {dirname} function: run_jt_is_journal +- when: I invoke jt new "{title:text}" --editor=none --dirname={dirname} + function: run_jt_new + +- when: I invoke jt finish --dirname={dirname} + function: run_jt_finish + +- when: I edit draft {draftno} in {dirname} to also contain "{text}" + function: edit_draft + - then: directory {dirname} exists function: is_directory - then: output contains "(?P.*)" regex: true function: output_contains + +- then: there are no drafts in {dirname} + function: journal_has_no_drafts + +- then: there is one draft in {dirname} + function: journal_has_one_draft + +- then: draft {draftno} in {dirname} contains "{pattern}" + function: draft_contains_string + +- then: there are no journal entries in {dirname} + function: journal_has_no_entries + +- then: there is one journal entry in {dirname}, at {variable} + function: journal_has_one_entry + +- then: file <{variable}> contains "{pattern}" + function: file_contains -- cgit v1.2.1