github.com/raphaelreyna/latte@v0.11.2-0.20220317193248-98e2fcef4eef/internal/job/job_test.go (about)

     1  package job
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  type Test struct {
    12  	Name string
    13  	Env  *TestEnv
    14  
    15  	// Name of the .pdf file in the testing pdf assets folder to test final product against
    16  	ExpectedPDF    string
    17  	ExpectedToPass bool
    18  
    19  	// Needs to have keys "left" and "right", both of which have values which are two character strings
    20  	Delimiters map[string]string
    21  
    22  	// OnMissingKey valid values: 'error', 'zero', 'nothing'
    23  	OnMissingKey string
    24  
    25  	// Compiler valid values: "pdflatex", "latexmk"
    26  	Compiler string
    27  }
    28  
    29  func (t *Test) Run(tt *testing.T, root string) {
    30  	var err error
    31  
    32  	j := Job{}
    33  	j.AddResource(t.Env.Resources...)
    34  	j.Root = root
    35  	t.Env.Root = root
    36  
    37  	j.Opts = Options{
    38  		CC:           Compiler(t.Compiler),
    39  		OnMissingKey: MissingKeyOpt(t.OnMissingKey),
    40  		Delims: Delimiters{
    41  			Left:  t.Delimiters["left"],
    42  			Right: t.Delimiters["right"],
    43  		},
    44  	}
    45  	defer t.Env.Clean()
    46  
    47  	j.SourceChain, err = t.Env.SourceChain()
    48  	if err != nil {
    49  		tt.Fatal(err)
    50  	}
    51  
    52  	if err = j.GetTemplate(t.Env.TexFile); err != nil {
    53  		tt.Fatal(err)
    54  	}
    55  
    56  	if err = j.GetDetails(t.Env.DtlsFile); err != nil {
    57  		tt.Fatal(err)
    58  	}
    59  
    60  	pdfLocation, err := j.Compile(context.Background())
    61  	if err != nil {
    62  		tt.Fatal(err)
    63  	}
    64  
    65  	if pdfLocation == "" {
    66  		tt.Fatal("resulted in empty pdf location")
    67  	}
    68  }
    69  
    70  func TestJob_Compile(t *testing.T) {
    71  	// Create temp dir for testing
    72  	currDir, err := os.Getwd()
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  
    77  	tests := []Test{
    78  		{
    79  			Name:           "Basic",
    80  			Delimiters:     map[string]string{"left": "#!", "right": "!#"},
    81  			ExpectedPDF:    "hello-world_alice.pdf",
    82  			ExpectedToPass: true,
    83  			Env: &TestEnv{
    84  				TexFile:   "hello-world.tex",
    85  				DtlsFile:  "hello-world_alice.json",
    86  				Resources: nil,
    87  			},
    88  		},
    89  		{
    90  			Name:           "Registered tex file",
    91  			Delimiters:     map[string]string{"left": "#!", "right": "!#"},
    92  			ExpectedPDF:    "hello-world_alice.pdf",
    93  			ExpectedToPass: true,
    94  			Env: &TestEnv{
    95  				TexFile:    "hello-world.tex",
    96  				TexFileLoc: 1,
    97  				DtlsFile:   "hello-world_alice.json",
    98  				Resources:  nil,
    99  			},
   100  		},
   101  	}
   102  
   103  	for _, test := range tests {
   104  		t.Run(test.Name, func(tt *testing.T) {
   105  			testingDir, err := ioutil.TempDir(filepath.Join(currDir, "../../testing"), "testingTmp")
   106  			if err != nil {
   107  				t.Fatal(err)
   108  			}
   109  			err = os.Chdir(testingDir)
   110  			if err != nil {
   111  				t.Fatal(err)
   112  			}
   113  			test.Run(tt, testingDir)
   114  			os.Chdir("../")
   115  			os.RemoveAll(testingDir)
   116  		})
   117  	}
   118  }