manus-tracker

tracker for scientific manuscripts
git clone git://src.adamsgaard.dk/manus-tracker # fast
git clone https://src.adamsgaard.dk/manus-tracker.git # slow
Log | Files | Refs | README Back to index

scan_test.go (4709B)


      1 package scan
      2 
      3 import (
      4 	"os"
      5 	"os/exec"
      6 	"path/filepath"
      7 	"slices"
      8 	"testing"
      9 	"time"
     10 )
     11 
     12 func write(t *testing.T, p, s string) {
     13 	t.Helper()
     14 	os.MkdirAll(filepath.Dir(p), 0o755)
     15 	if err := os.WriteFile(p, []byte(s), 0o644); err != nil {
     16 		t.Fatal(err)
     17 	}
     18 }
     19 
     20 func fixture(t *testing.T) string {
     21 	root := t.TempDir()
     22 	write(t, filepath.Join(root, "BIBnew.bib"), "@article{Shared2000, title={s}, doi={10.5555/s}}\n@article{NoDoi2001, title={n}}\n")
     23 	ms := filepath.Join(root, "paper")
     24 	write(t, filepath.Join(ms, "Makefile"), "NAME=paper\n\n$(NAME).pdf: $(DEPS)\n\tlatexmk\nfigures/%.pdf: figures/%.svg\n\tx\nauto: x\nCC := cc\n.PHONY: auto\n")
     25 	write(t, filepath.Join(ms, "paper.tex"), "\\documentclass{article}\n\\input{sec}\n\\cite{Shared2000,Local1}\n\\nocite{*}\n% \\cite{Commented}\n\\todo{fix intro}\n\\bibliography{BIBnew,methods}\n")
     26 	write(t, filepath.Join(ms, "sec.tex"), "See \\citep[p.~2]{NoDoi2001} and \\citet{Missing9}. % TODO: tighten\n")
     27 	write(t, filepath.Join(ms, "methods.bib"), "@article{Local1, doi={10.5555/l}}\n@misc{Unused1, title={u}}\n")
     28 	write(t, filepath.Join(ms, "figures", "a.svg"), "<svg/>")
     29 	write(t, filepath.Join(ms, "figures", "a.pdf"), "%PDF")
     30 	write(t, filepath.Join(ms, "figures", "b.pdf"), "%PDF")
     31 	write(t, filepath.Join(ms, "fig5-CM-LCJJPW03NP.py"), "")
     32 	write(t, filepath.Join(ms, "main copy.pdf"), "")
     33 	return root
     34 }
     35 
     36 func TestDetectNested(t *testing.T) {
     37 	root := t.TempDir()
     38 	write(t, filepath.Join(root, "ms", "rev1", "response.tex"), "\\documentclass{article}")
     39 	write(t, filepath.Join(root, "ms", "repo", "paper0.tex"), "\\documentclass{article}")
     40 	os.MkdirAll(filepath.Join(root, "ms", "repo", ".git"), 0o755)
     41 	d := Detect(filepath.Join(root, "ms"))
     42 	if d != (Detected{Kind: "latex", Src: "repo", Main: "paper0.tex", Repo: "repo"}) {
     43 		t.Fatalf("detected %+v", d)
     44 	}
     45 	write(t, filepath.Join(root, "doc", "old.docx"), "")
     46 	write(t, filepath.Join(root, "doc", "new.odt"), "")
     47 	future := time.Now().Add(time.Hour)
     48 	os.Chtimes(filepath.Join(root, "doc", "new.odt"), future, future)
     49 	if d := Detect(filepath.Join(root, "doc")); d.Kind != "odt" || d.Main != "new.odt" || d.Src != "." || d.Repo != "" {
     50 		t.Fatalf("detected %+v", d)
     51 	}
     52 }
     53 
     54 func TestDetectMakefileName(t *testing.T) {
     55 	root := fixture(t)
     56 	if d := Detect(filepath.Join(root, "paper")); d.Main != "paper.tex" || d.Kind != "latex" {
     57 		t.Fatalf("detected %+v", d)
     58 	}
     59 }
     60 
     61 func TestMakeTargets(t *testing.T) {
     62 	got := MakeTargets(filepath.Join(fixture(t), "paper"))
     63 	if !slices.Equal(got, []string{"auto"}) {
     64 		t.Fatalf("targets = %v", got)
     65 	}
     66 }
     67 
     68 func TestFigures(t *testing.T) {
     69 	got := Figures(filepath.Join(fixture(t), "paper"))
     70 	if len(got) != 2 || got[0] != (Figure{"figures/a", "figures/a.pdf", "figures/a.svg"}) || got[1].Preview != "figures/b.pdf" {
     71 		t.Fatalf("figures = %+v", got)
     72 	}
     73 }
     74 
     75 func TestTeXAndBib(t *testing.T) {
     76 	root := fixture(t)
     77 	src := filepath.Join(root, "paper")
     78 	tex := ParseTeX(src, "paper.tex")
     79 	if !slices.Equal(slices.Sorted(slices.Values(tex.Cites)), []string{"Local1", "Missing9", "NoDoi2001", "Shared2000"}) {
     80 		t.Fatalf("cites = %v", tex.Cites)
     81 	}
     82 	if len(tex.Todos) != 2 || tex.Todos[0].File != "sec.tex" || tex.Todos[1].Text != "fix intro" {
     83 		t.Fatalf("todos = %+v", tex.Todos)
     84 	}
     85 	r := CheckBib(root, src, tex)
     86 	if r.Files[0] != filepath.Join(root, "BIBnew.bib") {
     87 		t.Fatalf("files = %v", r.Files)
     88 	}
     89 	if !slices.Equal(r.Missing, []string{"Missing9"}) || !slices.Equal(r.Unused, []string{"Unused1"}) || !slices.Equal(r.NoDOI, []string{"NoDoi2001"}) {
     90 		t.Fatalf("report = %+v", r)
     91 	}
     92 }
     93 
     94 func TestConflictsAndPDF(t *testing.T) {
     95 	root := fixture(t)
     96 	src := filepath.Join(root, "paper")
     97 	if c := Conflicts(src); len(c) != 2 {
     98 		t.Fatalf("conflicts = %v", c)
     99 	}
    100 	if p := PDFStatus(src, "paper.tex"); !p.Missing {
    101 		t.Fatal("expected missing pdf")
    102 	}
    103 	write(t, filepath.Join(src, "paper.pdf"), "%PDF")
    104 	past := time.Now().Add(-time.Hour)
    105 	os.Chtimes(filepath.Join(src, "paper.pdf"), past, past)
    106 	if p := PDFStatus(src, "paper.tex"); p.Missing || !p.Stale {
    107 		t.Fatalf("pdf = %+v", p)
    108 	}
    109 }
    110 
    111 func TestGitInfo(t *testing.T) {
    112 	if _, err := exec.LookPath("git"); err != nil {
    113 		t.Skip("git not available")
    114 	}
    115 	dir := t.TempDir()
    116 	if GitInfo(dir, 5) != nil {
    117 		t.Fatal("non-repo should give nil")
    118 	}
    119 	for _, a := range [][]string{{"init", "-q"}, {"-c", "user.email=t@example.org", "-c", "user.name=t", "commit", "-q", "--allow-empty", "-m", "first"}} {
    120 		if out, err := exec.Command("git", append([]string{"-C", dir}, a...)...).CombinedOutput(); err != nil {
    121 			t.Fatalf("%v %s", err, out)
    122 		}
    123 	}
    124 	write(t, filepath.Join(dir, "x"), "")
    125 	g := GitInfo(dir, 5)
    126 	if g == nil || g.Dirty != 1 || len(g.Commits) != 1 || g.Commits[0].Subject != "first" {
    127 		t.Fatalf("git = %+v", g)
    128 	}
    129 }