From 67ea26196dd5b14267806dee882e3f55c5bbae30 Mon Sep 17 00:00:00 2001 From: Felipe Martin <812088+fmartingr@users.noreply.github.com> Date: Mon, 25 Sep 2023 22:33:35 +0200 Subject: [PATCH] test: create a comicinfo file and lint it against schema (#2) --- .github/workflows/test.yml | 3 + .gitignore | 4 ++ schema_test.go | 110 +++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 .gitignore create mode 100644 schema_test.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 23c405d..f2b4cb0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,6 +16,9 @@ jobs: steps: - uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0 + - name: Install libxml2-utils for xmllint + run: sudo apt-get install libxml2-utils + - name: Setup go uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0 with: diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b51c1f8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +ComicInfo.xsd +.DS_Store +schema.xsd +volume diff --git a/schema_test.go b/schema_test.go new file mode 100644 index 0000000..6a540c5 --- /dev/null +++ b/schema_test.go @@ -0,0 +1,110 @@ +package comicinfo + +import ( + "io" + "net/http" + "os" + "os/exec" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +const schemaURL = "https://raw.githubusercontent.com/anansi-project/comicinfo/main/schema/v2.0/ComicInfo.xsd" + +func TestFullSchema(t *testing.T) { + // Download the schema from the URL into a temporary file + tmpDir := t.TempDir() + schemaPath := filepath.Join(tmpDir, "schema.xsd") + + resp, err := http.Get(schemaURL) + if err != nil { + t.Errorf("error downloading schema: %s", err) + } + + defer resp.Body.Close() + + out, err := os.Create(schemaPath) + if err != nil { + t.Errorf("error creating schema file: %s", err) + } + + defer out.Close() + + _, err = io.Copy(out, resp.Body) + if err != nil { + t.Errorf("error writing schema file: %s", err) + } + + // Create a new ComicInfo struct with all the fields filled + ci := NewComicInfo() + ci.Title = "Test" + ci.Series = "Test" + ci.Number = "1" + ci.Count = 1 + ci.Volume = 1 + ci.AlternateSeries = "Test" + ci.AlternateNumber = "1" + ci.AlternateCount = 1 + ci.Summary = "Test" + ci.Notes = "Test" + ci.Year = 2021 + ci.Month = 1 + ci.Day = 1 + ci.Writer = "Test" + ci.Penciller = "Test" + ci.Inker = "Test" + ci.Colorist = "Test" + ci.Letterer = "Test" + ci.CoverArtist = "Test" + ci.Editor = "Test" + ci.Publisher = "Test" + ci.Imprint = "Test" + ci.Genre = "Test" + ci.Web = "Test" + ci.PageCount = 1 + ci.LanguageISO = "en" + ci.Format = "Test" + ci.BlackAndWhite = Yes + ci.Manga = MangaYes + ci.Characters = "Test" + ci.Teams = "Test" + ci.Locations = "Test" + ci.ScanInformation = "Test" + ci.StoryArc = "Test" + ci.SeriesGroup = "Test" + ci.AgeRating = AgeRatingEveryone + ci.MainCharacterOrTeam = "Test" + ci.Review = "Test" + ci.Pages.Pages = append(ci.Pages.Pages, ComicPageInfo{ + Image: 1, + Type: ComicPageTypeEditorial, + DoublePage: false, + ImageSize: 999, + Key: "?", + Bookmark: "yes", + ImageWidth: 20, + ImageHeight: 21, + }) + ci.CommunityRating = 5.0 + ci.MainCharacterOrTeam = "Test" + ci.Review = "Test" + + // Write the ComicInfo struct to a temporary file + ciPath := filepath.Join(tmpDir, "comicinfo.xml") + + err = Write(ci, ciPath) + if err != nil { + t.Errorf("error writing ComicInfo: %v", err) + } + + // Validate the schema running xmllint in a process for comicinfo.xml + cmd := exec.Command("xmllint", "--noout", "--schema", schemaPath, ciPath) + output, err := cmd.CombinedOutput() + if err != nil { + t.Errorf("error validating ComicInfo: %s\n\nValidator error: %s", err, output) + } + + require.NoError(t, cmd.Err, output) +}