github.com/karsthammer/dnscontrol@v0.2.8/pkg/js/js_test.go (about)

     1  package js
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  	"unicode"
    10  
    11  	"github.com/tdewolff/minify"
    12  	minjson "github.com/tdewolff/minify/json"
    13  )
    14  
    15  const (
    16  	testDir  = "pkg/js/parse_tests"
    17  	errorDir = "pkg/js/error_tests"
    18  )
    19  
    20  func init() {
    21  	os.Chdir("../..") // go up a directory so we helpers.js is in a consistent place.
    22  }
    23  
    24  func TestParsedFiles(t *testing.T) {
    25  	files, err := ioutil.ReadDir(testDir)
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	for _, f := range files {
    30  		// run all js files that start with a number. Skip others.
    31  		if filepath.Ext(f.Name()) != ".js" || !unicode.IsNumber(rune(f.Name()[0])) {
    32  			continue
    33  		}
    34  		m := minify.New()
    35  		m.AddFunc("json", minjson.Minify)
    36  		t.Run(f.Name(), func(t *testing.T) {
    37  			content, err := ioutil.ReadFile(filepath.Join(testDir, f.Name()))
    38  			if err != nil {
    39  				t.Fatal(err)
    40  			}
    41  			conf, err := ExecuteJavascript(string(content), true)
    42  			if err != nil {
    43  				t.Fatal(err)
    44  			}
    45  			actualJSON, err := json.MarshalIndent(conf, "", "  ")
    46  			if err != nil {
    47  				t.Fatal(err)
    48  			}
    49  			actualJSON, err = m.Bytes("json", actualJSON)
    50  			if err != nil {
    51  				t.Fatal(err)
    52  			}
    53  			expectedFile := filepath.Join(testDir, f.Name()[:len(f.Name())-3]+".json")
    54  			expectedData, err := ioutil.ReadFile(expectedFile)
    55  			if err != nil {
    56  				t.Fatal(err)
    57  			}
    58  
    59  			expectedJSON, err := m.String("json", string(expectedData))
    60  			if err != nil {
    61  				t.Fatal(err)
    62  			}
    63  			es := string(expectedJSON)
    64  			as := string(actualJSON)
    65  			if es != as {
    66  				t.Errorf("Expected and actual json don't match: %s", f.Name())
    67  				t.Logf("Expected(%v): %s", len(es), es)
    68  				t.Logf("Actual  (%v): %s", len(as), as)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  func TestErrors(t *testing.T) {
    75  	tests := []struct{ desc, text string }{
    76  		{"old dsp style", `D("foo.com","reg","dsp")`},
    77  		{"MX no priority", `D("foo.com","reg",MX("@","test."))`},
    78  		{"MX reversed", `D("foo.com","reg",MX("@","test.", 5))`},
    79  		{"CF_REDIRECT With comma", `D("foo.com","reg",CF_REDIRECT("foo.com,","baaa"))`},
    80  		{"CF_TEMP_REDIRECT With comma", `D("foo.com","reg",CF_TEMP_REDIRECT("foo.com","baa,a"))`},
    81  		{"Bad cidr", `D(reverse("foo.com"), "reg")`},
    82  		{"Dup domains", `D("example.org", "reg"); D("example.org", "reg")`},
    83  		{"Bad NAMESERVER", `D("example.com","reg", NAMESERVER("@","ns1.foo.com."))`},
    84  	}
    85  	for _, tst := range tests {
    86  		t.Run(tst.desc, func(t *testing.T) {
    87  			if _, err := ExecuteJavascript(tst.text, true); err == nil {
    88  				t.Fatal("Expected error but found none")
    89  			}
    90  		})
    91  
    92  	}
    93  }