github.com/everdrone/grab@v0.1.7-0.20230416223925-40674b995521/cmd/check_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/everdrone/grab/internal/utils"
     9  	tu "github.com/everdrone/grab/testutils"
    10  )
    11  
    12  const fileOk = `
    13  global {
    14  	location = "/home/user/Downloads/grab"
    15  }
    16  
    17  site "example" {
    18  	test = ":\\/\\/example\\.com"
    19  
    20  	asset "image" {
    21  		pattern  = "<img\\ssrc=\"([^\"]+)"
    22  		capture  = 1
    23  		find_all = true
    24  	}
    25  
    26  	asset "video" {
    27  		pattern  = "<video\\ssrc=\"(?P<video_url>[^\"]+)"
    28  		capture  = "video_url"
    29  		find_all = true
    30  	}
    31  }
    32  `
    33  
    34  const fileInvalid = `
    35  global {
    36  	location = "/home/user/Downloads/grab"
    37  }
    38  `
    39  
    40  func TestCheckCmd(t *testing.T) {
    41  	initialWd, _ := os.Getwd()
    42  	defer func() {
    43  		_ = os.Chdir(initialWd)
    44  	}()
    45  
    46  	root := tu.GetOSRoot()
    47  	utils.Fs, utils.Io, utils.Wd = tu.SetupMemMapFs(root)
    48  
    49  	utils.Fs.MkdirAll("/other/directory", os.ModePerm)
    50  	utils.Fs.MkdirAll("/tmp/test/config/nested", os.ModePerm)
    51  	utils.Io.WriteFile(utils.Fs, "/tmp/test/config/grab.hcl", []byte(fileOk), os.ModePerm)
    52  	utils.Io.WriteFile(utils.Fs, "/tmp/test/config/invalid.hcl", []byte(fileInvalid), os.ModePerm)
    53  
    54  	tests := []struct {
    55  		Name         string
    56  		Wd           string
    57  		Args         []string
    58  		WantContains string
    59  		WantErr      string
    60  		HasErrors    bool
    61  	}{
    62  		{
    63  			Name:         "ok nested",
    64  			Wd:           "/tmp/test/config/nested",
    65  			Args:         []string{},
    66  			WantContains: "ok\n",
    67  			WantErr:      "",
    68  			HasErrors:    false,
    69  		},
    70  		{
    71  			Name:         "ok same directory",
    72  			Wd:           "/tmp/test/config",
    73  			Args:         []string{},
    74  			WantContains: "ok\n",
    75  			WantErr:      "",
    76  			HasErrors:    false,
    77  		},
    78  		{
    79  			Name:         "ok quiet",
    80  			Wd:           "/tmp/test/config/nested",
    81  			Args:         []string{"-q"},
    82  			WantContains: "",
    83  			WantErr:      "",
    84  			HasErrors:    false,
    85  		},
    86  		{
    87  			Name:         "not found",
    88  			Wd:           "/other/directory",
    89  			Args:         []string{},
    90  			WantContains: "",
    91  			WantErr: `╷ Error: could not resolve config file
    92  ╵   could not resolve grab.hcl
    93  `,
    94  			HasErrors: true,
    95  		},
    96  		{
    97  			Name:         "invalid args",
    98  			Wd:           "/tmp/test/config/nested",
    99  			Args:         []string{"--unknown"},
   100  			WantContains: "Usage:",
   101  			WantErr:      "Error: unknown flag: --unknown\n",
   102  			HasErrors:    true,
   103  		},
   104  		{
   105  			Name:         "missing global",
   106  			Wd:           "/tmp/test/config/nested",
   107  			Args:         []string{"-c", "/tmp/test/config/invalid.hcl"},
   108  			WantContains: "",
   109  			WantErr: `╷ Error: Insufficient site blocks
   110  │   At least 1 "site" blocks are required.
   111  ╵   /tmp/test/config/invalid.hcl:1,1-1
   112  `,
   113  			HasErrors: true,
   114  		},
   115  		{
   116  			Name:         "file does not exist",
   117  			Wd:           "/tmp/test/config/nested",
   118  			Args:         []string{"-c", "notFound.hcl"},
   119  			WantContains: "",
   120  			WantErr: `╷ Error: could not read config file
   121  ╵   open notFound.hcl:`,
   122  			HasErrors: true,
   123  		},
   124  	}
   125  
   126  	for _, tt := range tests {
   127  		t.Run(tt.Name, func(t *testing.T) {
   128  			args := []string{"config", "check"}
   129  
   130  			func() {
   131  				utils.Wd = tt.Wd
   132  			}()
   133  
   134  			c, got, gotErr, err := tu.ExecuteCommandErr(RootCmd, append(args, tt.Args...)...)
   135  			if err != nil && !tt.HasErrors {
   136  				t.Errorf("unexpected error: %v", err)
   137  			}
   138  
   139  			if c.Name() != CheckCmd.Name() {
   140  				t.Errorf("got: '%s', want: %s", c.Name(), CheckCmd.Name())
   141  			}
   142  			if !strings.Contains(got, tt.WantContains) {
   143  				t.Errorf("got: %s, does not contain: %s", got, tt.WantContains)
   144  			}
   145  			if !strings.HasPrefix(gotErr, tt.WantErr) {
   146  				t.Errorf("got: %s, want: %s", gotErr, tt.WantErr)
   147  			}
   148  		})
   149  	}
   150  }