github.com/teknogeek/dnscontrol/v2@v2.10.1-0.20200227202244-ae299b55ba42/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  			conf, err := ExecuteJavascript(string(filepath.Join(testDir, f.Name())), true)
    38  			if err != nil {
    39  				t.Fatal(err)
    40  			}
    41  			actualJSON, err := json.MarshalIndent(conf, "", "  ")
    42  			if err != nil {
    43  				t.Fatal(err)
    44  			}
    45  			actualJSON, err = m.Bytes("json", actualJSON)
    46  			if err != nil {
    47  				t.Fatal(err)
    48  			}
    49  			expectedFile := filepath.Join(testDir, f.Name()[:len(f.Name())-3]+".json")
    50  			expectedData, err := ioutil.ReadFile(expectedFile)
    51  			if err != nil {
    52  				t.Fatal(err)
    53  			}
    54  
    55  			expectedJSON, err := m.String("json", string(expectedData))
    56  			if err != nil {
    57  				t.Fatal(err)
    58  			}
    59  			es := string(expectedJSON)
    60  			as := string(actualJSON)
    61  			if es != as {
    62  				t.Errorf("Expected and actual json don't match: %s", f.Name())
    63  				t.Logf("Expected(%v): %s", len(es), es)
    64  				t.Logf("Actual  (%v): %s", len(as), as)
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  func TestErrors(t *testing.T) {
    71  	tests := []struct{ desc, text string }{
    72  		{"old dsp style", `D("foo.com","reg","dsp")`},
    73  		{"MX no priority", `D("foo.com","reg",MX("@","test."))`},
    74  		{"MX reversed", `D("foo.com","reg",MX("@","test.", 5))`},
    75  		{"CF_REDIRECT With comma", `D("foo.com","reg",CF_REDIRECT("foo.com,","baaa"))`},
    76  		{"CF_TEMP_REDIRECT With comma", `D("foo.com","reg",CF_TEMP_REDIRECT("foo.com","baa,a"))`},
    77  		{"Bad cidr", `D(reverse("foo.com"), "reg")`},
    78  		{"Dup domains", `D("example.org", "reg"); D("example.org", "reg")`},
    79  		{"Bad NAMESERVER", `D("example.com","reg", NAMESERVER("@","ns1.foo.com."))`},
    80  	}
    81  	for _, tst := range tests {
    82  		t.Run(tst.desc, func(t *testing.T) {
    83  			if _, err := ExecuteJavascript(tst.text, true); err == nil {
    84  				t.Fatal("Expected error but found none")
    85  			}
    86  		})
    87  
    88  	}
    89  }