Skip to content

Commit

Permalink
stringtable: allow bin with just warnings (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettMayson authored Dec 20, 2024
1 parent 9f04af8 commit f2c2356
Show file tree
Hide file tree
Showing 35 changed files with 20,824 additions and 93 deletions.
4 changes: 2 additions & 2 deletions bin/src/modules/stringtables/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use hemtt_stringtable::{
Project,
};
use hemtt_workspace::{
reporting::{Code, Diagnostic},
reporting::{Code, Diagnostic, Severity},
WorkspacePath,
};

Expand Down Expand Up @@ -69,7 +69,7 @@ impl Module for Stringtables {

for stringtable in stringtables {
let codes = lint_one(&stringtable, Some(ctx.config()));
if codes.is_empty() {
if !codes.iter().any(|c| c.severity() == Severity::Error) {
convert_stringtable(&stringtable.0, &stringtable.1);
}
report.extend(codes);
Expand Down
2 changes: 1 addition & 1 deletion libs/stringtable/src/analyze/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hemtt_common::config::ProjectConfig;
use hemtt_workspace::{lint::LintManager, lint_manager, reporting::Codes};
use lints::_01_sorted::StringtableData;
use lints::l01_sorted::StringtableData;

pub mod lints {
automod::dir!(pub "src/analyze/lints");
Expand Down
1 change: 1 addition & 0 deletions libs/stringtable/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl Package {

pub fn sort(&mut self) {
self.keys.sort_by(|a, b| a.id().cmp(b.id()));
self.containers.sort_by(|a, b| a.name().cmp(b.name()));
for container in &mut self.containers {
container.sort();
}
Expand Down
3 changes: 3 additions & 0 deletions libs/stringtable/src/rapify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ struct Translation {
have_unique: bool,
}

/// Converts a stringtable.xml to a stringtable.bin
///
/// # Panics
/// If the files can't be read or written from the vfs
pub fn convert_stringtable(project: &Project, xml_path: &WorkspacePath) {
let result = rapify(project);

Expand Down
18 changes: 0 additions & 18 deletions libs/stringtable/tests/ace_arsenal.rs

This file was deleted.

51 changes: 51 additions & 0 deletions libs/stringtable/tests/bin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![allow(clippy::unwrap_used)]

use std::io::BufReader;

use hemtt_stringtable::{rapify::rapify, Project};

#[test]
fn bin_pass() {
let stringtable = Project::from_reader(BufReader::new(
std::fs::File::open("tests/bin/pass.xml").unwrap(),
))
.unwrap();
// Has 2 languages with unique translations
let bin = rapify(&stringtable);
assert!(bin.is_some());
insta::assert_debug_snapshot!(bin.unwrap());
}

#[test]
fn bin_containers() {
let stringtable = Project::from_reader(BufReader::new(
std::fs::File::open("tests/bin/containers.xml").unwrap(),
))
.unwrap();
// Has 2 languages with unique translations
let bin = rapify(&stringtable);
assert!(bin.is_some());
insta::assert_debug_snapshot!(bin.unwrap());
}

#[test]
fn bin_invalid() {
let stringtable = Project::from_reader(BufReader::new(
std::fs::File::open("tests/bin/invalid.xml").unwrap(),
))
.unwrap();
// Cannot be binnerized
let bin = rapify(&stringtable);
assert!(bin.is_none());
}

#[test]
fn bin_unescaped() {
let stringtable = Project::from_reader(BufReader::new(
std::fs::File::open("tests/bin/unescaped.xml").unwrap(),
))
.unwrap();
// Cannot be binnerized
let bin = rapify(&stringtable);
assert!(bin.is_none());
}
21 changes: 21 additions & 0 deletions libs/stringtable/tests/bin/containers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project name="HEMTT">
<Package name="Test">
<Key ID="A1">
<Original>Origin</Original>
</Key>
<Key ID="A2">
<German>DE</German>
<English>EN</English>
</Key>
<Container name="A3">
<Key ID="A4">
<Original>Origin</Original>
</Key>
<Key ID="A5">
<German>DE</German>
<English>EN</English>
</Key>
</Container>
</Package>
</Project>
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions libs/stringtable/tests/bin/unescaped.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<Project name="HEMTT">
<Package name="Test">
<Key ID="A1">
<Original>A & B</Original>
</Key>
<Key ID="A2">
<German>DE</German>
<English>EN</English>
</Key>
</Package>
</Project>
23 changes: 0 additions & 23 deletions libs/stringtable/tests/comments.rs

This file was deleted.

21 changes: 0 additions & 21 deletions libs/stringtable/tests/gh822.rs

This file was deleted.

54 changes: 54 additions & 0 deletions libs/stringtable/tests/lints.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#![allow(clippy::unwrap_used)]

use std::io::BufReader;

use hemtt_stringtable::{
analyze::{lint_all, lint_one},
Project,
};
use hemtt_workspace::{
reporting::{Codes, WorkspaceFiles},
LayerType,
};

const ROOT: &str = "tests/lints/";

macro_rules! lint {
($dir:ident) => {
paste::paste! {
#[test]
fn [<simple_ $dir>]() {
insta::assert_snapshot!(lint(stringify!($dir)));
}
}
};
}

lint!(l01_sorted);

fn lint(file: &str) -> String {
let folder = std::path::PathBuf::from(ROOT);
let workspace = hemtt_workspace::Workspace::builder()
.physical(&folder, LayerType::Source)
.finish(None, false, &hemtt_common::config::PDriveOption::Disallow)
.unwrap();
let source = workspace.join(format!("{file}.xml")).unwrap();
let workspace_files = WorkspaceFiles::new();

let existing = source.read_to_string().expect("vfs issue");
let stringtable = Project::from_reader(BufReader::new(existing.as_bytes())).unwrap();

let mut codes: Codes = Vec::new();
codes.extend(lint_one(
&(stringtable.clone(), workspace.clone(), existing.clone()),
None,
));
codes.extend(lint_all(&vec![(stringtable, workspace, existing)], None));

codes
.iter()
.map(|e| e.diagnostic().unwrap().to_string(&workspace_files))
.collect::<Vec<_>>()
.join("\n")
.replace('\r', "")
}
22 changes: 22 additions & 0 deletions libs/stringtable/tests/lints/l01_sorted.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<Project name="test">
<Package name="test">
<Key ID="A1">
<Original>Origin</Original>
</Key>
<Container name="B">
<Key ID="B1">
<Original>Origin</Original>
</Key>
<Key ID="A2">
<German>DE</German>
<English>EN</English>
</Key>
</Container>
<Container name="A">
<Key ID="A3">
<Original>Origin</Original>
</Key>
</Container>
</Package>
</Project>
Loading

0 comments on commit f2c2356

Please sign in to comment.