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
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ compass for ORE Studio's contract, which compass's mirrors.

* Status

| Field | Value |
|--------------+----------------------------------------|
| State | STARTED |
| Parent story | [[id:C1DC377F-4A1B-4131-B734-FE28DB63CF03][Serve the documents]] |
| Now | The export, its contract page and the ORE Studio evidence are done; the pull request is open for review. |
| Waiting on | Review of the pull request. |
| Field | Value |
|--------------+--------------------------------------------------------------------------------------|
| State | STARTED |
| Parent story | [[id:C1DC377F-4A1B-4131-B734-FE28DB63CF03][Serve the documents]] |
| Now | The export is merged; the fixes from its review are in a follow-up pull request. |
| Waiting on | Review of the follow-up pull request. |
| Next | ORE Studio adopts the contract page from the handoff; Serve the documents over HTTP. |
| Last touched | 2026-11-23 |
| Last touched | 2026-11-23 |

* Acceptance

Expand All @@ -61,8 +61,10 @@ compass for ORE Studio's contract, which compass's mirrors.
nothing. =exported_at= is set.
- Scenario pages are not modified.
- Text a tester typed cannot become org structure: a line org would read as
a heading or keyword is escaped in an example block, and a =|= in a table
cell is written =\vert{}=.
a heading or keyword is escaped in an example block, a =|= in a table
cell is written =\vert{}=, and a =[[=, a =]]= or the colon of a plain
link such as =id:X= in a cell, a caption or a link's description is
broken by a zero-width space, so it forms no link.
- The export's input is the campaign's rows and its output (path, bytes)
files; the rendering is a pure function of the rows, tested without a
database. No JSON.
Expand Down
10 changes: 7 additions & 3 deletions doc/meta/document_type_test_campaign.org
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ page carries only what is specific to =test_campaign=.
=[[file:test_campaign_<id>/<blob id>.<ext>]]= from the run or note that
holds it, with the step and caption.
- Escaping :: text a tester typed goes into an =example= block, with a
line org would read as a heading or a keyword escaped by a leading comma
as org escapes it; in a table cell it is on one line with each =|= written
=\vert{}=. Nothing a tester types becomes a heading.
line org would read as a heading or a keyword escaped as org escapes it,
by a comma after its indentation; in a table cell or a caption it is on
one line with each =|= written =\vert{}= and a zero-width space between
the two brackets of each =[[= and =]]=, as =org-link-make-string= writes
them, and before the colon of a plain link such as =id:X=. A page title
used as a link description is escaped the same way.
Nothing a tester types becomes a heading, a cell or a link.
- Scenario pages :: the export never writes a =test_scenario= page; the
results live on the campaign page.
- How to create :: =compass test export=; never by hand.
Expand Down
5 changes: 5 additions & 0 deletions projects/compass.cli/src/app/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ int run_test(const types::invocation& i, std::ostream& out,
i.environment);
const auto r = export_campaigns(c, i.checkout, environment,
i.test.story);
if (i.test.story && r.campaigns == 0 && r.skipped.empty()) {
err << "compass: test export: no test campaign of story "
<< *i.test.story << " on " << environment << "\n";
return exit_failure;
}
for (const auto& path : r.written)
out << "compass: wrote " << path << "\n";
out << std::format("compass: exported {} test campaigns of {}: {} "
Expand Down
37 changes: 32 additions & 5 deletions projects/compass.cli/src/app/test_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,62 @@
*/
#include "compass.cli/app/test_export.hpp"

#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <string>
#include <vector>
#include "compass.db/ops/campaign_page.hpp"
#include "compass.db/query/campaign_export.hpp"
#include "compass.platform/filesystem/new_file.hpp"

namespace compass::cli::app {

namespace {

/**
* @brief @p relative under @p checkout.
* @throws std::runtime_error when @p relative is absolute or climbs out of
* the checkout.
*/
std::filesystem::path inside(const std::filesystem::path& checkout,
const std::string& relative) {
const auto path = std::filesystem::path(relative).lexically_normal();
if (path.has_root_path() || path.empty() || *path.begin() == "..")
throw std::runtime_error("refusing to write " + relative +
", which is outside the checkout");
return checkout / path;
}

}

export_report export_campaigns(db::io::connection& c,
const std::filesystem::path& checkout, const std::string& environment,
const std::optional<std::string>& story) {
export_report out;
std::vector<std::int64_t> exported;
for (const auto& campaign : db::query::read_campaigns(c, environment,
story)) {
const auto snapshot = db::query::read_campaigns(c, environment, story);
for (const auto& campaign : snapshot.campaigns) {
if (campaign.story_path.empty()) {
out.skipped.push_back(campaign.story_id);
continue;
}
for (const auto& image : campaign.images)
out.not_png += image.media_type == "image/png" ? 0 : 1;
for (const auto& f : db::ops::render_campaign(campaign)) {
if (platform::filesystem::update_file(checkout / f.path, f.bytes))
const auto files = db::ops::render_campaign(campaign);
std::vector<std::filesystem::path> paths;
for (const auto& f : files)
paths.push_back(inside(checkout, f.path));
for (std::size_t i = 0; i < files.size(); ++i) {
const auto& f = files[i];
if (platform::filesystem::update_file(paths[i], f.bytes))
out.written.push_back(f.path);
else
++out.unchanged;
}
exported.push_back(campaign.id);
}
db::query::mark_exported(c, exported);
db::query::mark_exported(c, exported, snapshot.taken_at);
out.campaigns = exported.size();
return out;
}
Expand Down
102 changes: 97 additions & 5 deletions projects/compass.cli/tests/app_test_export_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <cstdint>
#include <filesystem>
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
Expand All @@ -27,6 +29,7 @@
#include "compass.cli/app/application.hpp"
#include "compass.db/io/connection.hpp"
#include "compass.db/query/campaign.hpp"
#include "compass.db/query/campaign_export.hpp"
#include "compass.testing/database/scoped_cluster.hpp"
#include "scoped_git_repo.hpp"

Expand Down Expand Up @@ -152,7 +155,14 @@ TEST_CASE("test export writes the campaign page and its screenshot into the "
const auto page = scoped_git_repo::read_file(s.at(page_path));
CHECK_THAT(page, ContainsSubstring("#+type: test_campaign\n"));
CHECK_THAT(page, ContainsSubstring("*** Run 1: failed\n"));
CHECK(fs::is_directory(s.at(story_folder + "/test_campaign_1")));
std::vector<fs::path> shots;
for (const auto& e : fs::directory_iterator(
s.at(story_folder + "/test_campaign_1")))
shots.push_back(e.path());
REQUIRE(shots.size() == 1);
CHECK(shots[0].extension() == ".png");
CHECK(scoped_git_repo::read_file(shots[0]) ==
std::string("\x89PNG\r\n\x1a\n", 8));
CHECK(scoped_git_repo::read_file(s.at(scenario_path)) == scenario);
compass::db::io::connection c(s.database);
CHECK(c.exec("SELECT exported_at IS NOT NULL FROM activity.campaign")
Expand Down Expand Up @@ -199,16 +209,98 @@ TEST_CASE("test export keeps the page's path and :ID: when the campaign "
std::to_string(note) + " on [[id:SC-1][Test Scenario: A]], step 2\n"));
}

TEST_CASE("test export of an environment with no campaign writes nothing, "
"and of one that does not exist fails", "[app][test_export]") {
TEST_CASE("test export of a story that has no campaign fails and writes "
"nothing, and of an environment that does not exist fails",
"[app][test_export]") {
campaign_repo s;
const auto none = s.command({"test", "export", "--environment", "main",
"--story", "STORY-2"});
CHECK(none.status == app::exit_success);
CHECK_THAT(none.out, ContainsSubstring("exported 0 test campaigns"));
CHECK(none.status == app::exit_failure);
CHECK_THAT(none.err, ContainsSubstring("no test campaign of story "
"STORY-2 on main"));
CHECK_FALSE(fs::exists(s.at(page_path)));
const auto missing = s.command({"test", "export", "--environment",
"nowhere"});
CHECK(missing.status == app::exit_failure);
CHECK_THAT(missing.err, ContainsSubstring("no environment named nowhere"));
}

TEST_CASE("test export --story writes only that story's campaign",
"[app][test_export]") {
campaign_repo s;
const std::string other = "doc/agile/versions/v0/sprint_01/s2";
s.repo.write(other + "/story.org", ":PROPERTIES:\n:ID: STORY-2\n:END:\n"
"#+title: Story: Two\n#+type: story\n");
s.repo.write(other + "/scenario_b.org", ":PROPERTIES:\n:ID: SC-2\n"
":END:\n#+title: Test Scenario: B\n#+type: test_scenario\n\n"
"* Scenario Info\n\n| Field | Value |\n|-------+-------|\n"
"| Parent story | [[id:STORY-2][Story: Two]] |\n\n"
"* Steps\n\n** Look\n\n- Look at it.\n");
REQUIRE(s.command({"sync", "--environment", "main"}).status == 0);
compass::db::types::start_run_request start;
start.environment = "main";
start.scenario_id = "SC-2";
start.tester = "ben";
REQUIRE(compass::db::query::start_run(s.c, start).campaign == 2);

const auto r = s.command({"test", "export", "--environment", "main",
"--story", "STORY-2"});
CHECK(r.status == app::exit_success);
CHECK_THAT(r.out, ContainsSubstring("exported 1 test campaigns of main: "
"1 files written"));
CHECK(fs::exists(s.at(other + "/test_campaign_2.org")));
CHECK_FALSE(fs::exists(s.at(page_path)));
CHECK(s.c.exec("SELECT id FROM activity.campaign "
"WHERE exported_at IS NOT NULL").value(0, 0) == "2");
}

TEST_CASE("test export of a campaign whose story page is gone warns, "
"writes nothing and leaves it unexported", "[app][test_export]") {
campaign_repo s;
fs::remove(s.at(story_folder + "/story.org"));
REQUIRE(s.command({"sync", "--environment", "main"}).status == 0);
const auto r = s.export_main();
CHECK(r.status == app::exit_findings);
CHECK_THAT(r.err, ContainsSubstring("compass: warning: the campaign of "
"story STORY-1 was not exported: main no longer holds the story's "
"page"));
CHECK_THAT(r.out, ContainsSubstring("exported 0 test campaigns"));
CHECK_FALSE(fs::exists(s.at(page_path)));
CHECK_FALSE(fs::exists(s.at(story_folder + "/test_campaign_1")));
CHECK(s.c.exec("SELECT exported_at IS NULL FROM activity.campaign")
.value(0, 0) == "t");
}

TEST_CASE("test export refuses a story page outside the checkout",
"[app][test_export]") {
campaign_repo s;
s.c.exec("UPDATE corpus.placement SET path = '../outside/story.org' "
"WHERE path = $1", {story_folder + "/story.org"});
const auto r = s.export_main();
CHECK(r.status == app::exit_failure);
CHECK_THAT(r.err, ContainsSubstring("outside the checkout"));
CHECK_FALSE(fs::exists(s.repo.checkout().parent_path() / "outside"));
CHECK(s.c.exec("SELECT exported_at IS NULL FROM activity.campaign")
.value(0, 0) == "t");
}

TEST_CASE("an export marks its campaigns exported at the time of its "
"snapshot, before a note recorded after it", "[app][test_export]") {
campaign_repo s;
const auto snapshot = compass::db::query::read_campaigns(s.c, "main",
std::nullopt);
REQUIRE(snapshot.campaigns.size() == 1);
compass::db::types::add_note_request n;
n.campaign = s.campaign;
n.kind = "defect";
n.text = "Recorded after the snapshot.";
n.tester = "ben";
compass::db::query::add_note(s.c, {{"defect"}}, n);
const std::vector<std::int64_t> ids{s.campaign};
compass::db::query::mark_exported(s.c, ids, snapshot.taken_at);
CHECK(s.c.exec("SELECT exported_at = $1::timestamptz "
"FROM activity.campaign", {snapshot.taken_at}).value(0, 0) == "t");
CHECK(s.c.exec("SELECT c.exported_at < n.created_at "
"FROM activity.campaign c JOIN activity.note n ON n.campaign = c.id")
.value(0, 0) == "t");
}
15 changes: 9 additions & 6 deletions projects/compass.db/include/compass.db/query/campaign_export.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,24 @@ namespace compass::db::query {

/**
* @brief The rows of every campaign of @p environment, or only that of the
* story whose :ID: is @p story, in id order, read in one snapshot. A
* campaign whose story the environment no longer holds has an empty
* story_path.
* story whose :ID: is @p story, in id order, read in one snapshot, with
* the time of that snapshot. A campaign whose story the environment no
* longer holds has an empty story_path.
* @throws unknown_environment when the environment does not exist.
* @throws io::error when a statement fails.
*/
[[nodiscard]] std::vector<types::campaign_rows> read_campaigns(
[[nodiscard]] types::campaign_snapshot read_campaigns(
io::connection& c, const std::string& environment,
const std::optional<std::string>& story);

/**
* @brief Sets exported_at of each campaign of @p ids to now.
* @brief Sets exported_at of each campaign of @p ids to @p taken_at, the
* time of the snapshot they were exported from, so nothing recorded after
* it counts as exported.
* @throws io::error when the statement fails.
*/
void mark_exported(io::connection& c, std::span<const std::int64_t> ids);
void mark_exported(io::connection& c, std::span<const std::int64_t> ids,
const std::string& taken_at);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ struct campaign_rows {
std::vector<exported_image> images;
};

/**
* @brief The campaigns one export reads, and the time of the snapshot it
* read them in, as PostgreSQL writes a timestamptz.
*/
struct campaign_snapshot {
std::string taken_at;
std::vector<campaign_rows> campaigns;
};

/**
* @brief A file the export writes: its path, relative to the checkout,
* and its exact bytes.
Expand Down
Loading
Loading