Skip to content

Commit

Permalink
feat: use git2 to initialize archive states
Browse files Browse the repository at this point in the history
Rely on git2 to initialize a git repo and add files to the git
repository. The framework needs to get more flexible so we can test out
different repository states.
  • Loading branch information
n-dusan committed Dec 8, 2023
1 parent a11724f commit ea397ab
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 49 deletions.
119 changes: 70 additions & 49 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ pub fn initialize_archive(archive_type: ArchiveType) -> Result<tempfile::TempDir
Err(err) => {
dbg!(&err);
let error_output_directory = path.clone().join(PathBuf::from("error_output_directory"));
std::fs::remove_dir_all(&error_output_directory);
std::fs::remove_dir_all(&error_output_directory).unwrap();
std::fs::rename(td.path(), &error_output_directory)
.expect("Failed to move temp directory");
eprintln!(
"{}", format!("Failed to remove '{error_output_directory:?}', please try to do that by hand. Original error: {err}")
"{}", format!("Failed to remove '{error_output_directory:?}', please try to remove directory by hand. Original error: {err}")
);
Err(err)
}
Expand All @@ -82,7 +82,8 @@ fn initialize_archive_basic(td: &TempDir) -> Result<()> {
org_name.into(),
None,
false,
);
)
.unwrap();
let stele = initialize_stele(
td.path().to_path_buf(),
org_name,
Expand All @@ -91,13 +92,13 @@ fn initialize_archive_basic(td: &TempDir) -> Result<()> {
DataRepositoryType::Rdf("rdf".into()),
DataRepositoryType::Xml("xml".into()),
DataRepositoryType::Xml("xml-codified".into()),
DataRepositoryType::Pdf("pdf".into()),
// DataRepositoryType::Pdf("pdf".into()),
],
)
.unwrap();
// let law = make_repository("make-law-repo.sh", &path).unwrap();
// let law_html = make_repository("make-law-html-repo.sh", &path).unwrap();
anyhow::bail!("Something went wrong!");
// anyhow::bail!("Something went wrong!");
Ok(())
}

Expand Down Expand Up @@ -129,71 +130,91 @@ pub fn init_auth_repository(
std::fs::create_dir_all(&path).unwrap();

let repo = GitRepository::init(&path).unwrap();
add_repositories_json(&repo, &path)?;

path.push("targets");

let content = r#"{
"repositories": {
"test_org/law-html": {
"custom": {
"type": "html",
"serve": "historical",
"location_regex": "/",
"routes": [".*"]
}
}
}
}"#;

repo.add_file(&path, "repositories.json", content).unwrap();
repo.commit(Some("targets/repositories.json"), "Add repositories.json")
.unwrap();
Ok(repo)
}

pub fn init_data_repositories(
path: &Path,
org_name: &str,
data_repositories: &[DataRepositoryType],
) -> Result<()> {
) -> Result<Vec<GitRepository>> {
let mut data_git_repositories: Vec<GitRepository> = Vec::new();
for data_type in data_repositories {
let mut path = path.to_path_buf();
path.push(format!("{}/law-{}", org_name, data_type.to_string()));
dbg!(&path);
std::fs::create_dir_all(&path).unwrap();
let repo = GitRepository::init(&path).unwrap();
match data_type {
DataRepositoryType::Html(name) => {
add_html(&repo, &path, name)?;
}
DataRepositoryType::Rdf(name) => {
init_rdf_repository(&repo, &path, name)?;
}
DataRepositoryType::Xml(name) => {
init_xml_repository(&repo, &path, name)?;
}
DataRepositoryType::Pdf(name) => {
init_pdf_repository(&repo, &path, name)?;
}
DataRepositoryType::Other(name) => {
init_other_repository(&repo, &path, name)?;
}
}
init_data_repository(&repo, data_type, None)?;
data_git_repositories.push(repo);
}
Ok(())
Ok(data_git_repositories)
}

fn add_repositories_json(repo: &GitRepository, path: &Path) -> Result<()> {
let mut path = path.to_path_buf();
path.push("targets");
let content = r#"{
"law": {
"custom": {
"type": "data",
"allow-unauthenticated-commits": true,
"serve": "law",
"routes": [
"law/{path:.*}"
],
"serve-prefix": "law",
"is-fallback": true
}
fn init_data_repository(
repo: &GitRepository,
data_type: &DataRepositoryType,
filename: Option<&str>,
) -> Result<()> {
let filename = if let Some(f) = filename {
f
} else {
match data_type {
DataRepositoryType::Html(_) => "index.html",
DataRepositoryType::Rdf(_) => "index.rdf",
DataRepositoryType::Xml(_) => "index.xml",
DataRepositoryType::Pdf(_) => "example.pdf",
DataRepositoryType::Other(_) => "example.other",
}
}"#;
};

repo.write_file(&path, "repositories.json", content)
.unwrap();
repo.commit("targets/repositories.json", "Add repositories.json")
.unwrap();
let file_content = static_file_content(filename);

for subdirectory in ["./", "./a/", "./a/b/", "./a/b/c", "./a/d/"] {
repo.add_file(
&repo.path.join(subdirectory),
filename,
file_content.as_str(),
)?;
}
repo.commit(None, "Add initial data")?;

let filename = {
let ext = Path::new(filename)
.extension()
.map_or("html", |ext| ext.to_str().map_or("", |ext_str| ext_str));

format!("c.{}", ext)
};
repo.add_file(&repo.path.join("./a/b/"), &filename, file_content.as_str())?;
repo.commit(None, "Update repository")?;
Ok(())
}
pub fn make_repository(script_name: &str, path: &Path) -> Result<()> {
archive_testtools::execute_script(script_name, path.canonicalize()?)?;
// TODO: return repository
Ok(())

fn static_file_content(filename: &str) -> String {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("tests/fixtures/static_files");
path.push(filename);
std::fs::read_to_string(path).unwrap()
}

/// Used to initialize the test environment for git micro-server.
Expand Down
Empty file.
Empty file.
Binary file added tests/fixtures/static_files/example.pdf
Binary file not shown.
16 changes: 16 additions & 0 deletions tests/fixtures/static_files/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>index.html</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="/static/main.css">
<script src="/static/main.js"></script>
</head>
<body>
<h1>index.html</h1>
<img src="/static/logo.png"/>
<p id="js"></p>
</body>
</html>
6 changes: 6 additions & 0 deletions tests/fixtures/static_files/index.rdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="https://open.law/">
<rdf:type rdf:resource="https://open.law/us/ngo/oll/_ontology/v0.1/ontology.owl#Library"/>
</rdf:Description>
</rdf:RDF>
16 changes: 16 additions & 0 deletions tests/fixtures/static_files/index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version='1.0' encoding='utf-8'?>
<library xmlns="https://open.law/schemas/library" xmlns:codified="https://open.law/schemas/codified" xmlns:codify="https://open.law/schemas/codify" xmlns:xi="http://www.w3.org/2001/XInclude">
<heading>Law Library</heading>
<meta>
<description/>
<contact>
<email>[email protected]</email>
</contact>
<history-start>2023-12-22</history-start>
</meta>
<collection name="resolutions">
<heading>Resolutions</heading>
<xi:include href=".relative-path/2024/index.xml"/>
<xi:include href=".relative-path/2023/index.xml"/>
</collection>
</library>

0 comments on commit ea397ab

Please sign in to comment.