github.com/stillson/go-wf@v0.0.0-20240502203501-5781d3fae028/main_test.go (about)

     1  /*
     2   * Copyright (c) 2024. Christopher Stillson <stillson@gmail.com>
     3   *
     4   * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     5   *
     6   * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     7   * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     8   * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
     9   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    10   */
    11  
    12  package main
    13  
    14  import (
    15  	"bytes"
    16  	"flag"
    17  	"io"
    18  	"os"
    19  	"path/filepath"
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/stillson/go-wf/rcparse"
    24  )
    25  
    26  func TestParseArgs(t *testing.T) {
    27  	tests := []struct {
    28  		name    string
    29  		newArgs []string
    30  		verbose bool
    31  		time    bool
    32  		dump    bool
    33  		wfFile  string
    34  		rules   bool
    35  	}{
    36  		{
    37  			name:    "test1",
    38  			newArgs: []string{"wf"},
    39  			verbose: false,
    40  			time:    false,
    41  			dump:    false,
    42  			wfFile:  ".workflow.yaml",
    43  			rules:   false,
    44  		},
    45  		{
    46  			name:    "test2",
    47  			newArgs: []string{"wf", "-f", "TESTNAME"},
    48  			verbose: false,
    49  			time:    false,
    50  			dump:    false,
    51  			wfFile:  "TESTNAME",
    52  			rules:   false,
    53  		},
    54  		{
    55  			name:    "test3",
    56  			newArgs: []string{"wf", "-V"},
    57  			verbose: true,
    58  			time:    false,
    59  			dump:    false,
    60  			wfFile:  ".workflow.yaml",
    61  			rules:   false,
    62  		},
    63  
    64  		{
    65  			name:    "test4",
    66  			newArgs: []string{"wf", "-t"},
    67  			verbose: false,
    68  			time:    true,
    69  			dump:    false,
    70  			wfFile:  ".workflow.yaml",
    71  			rules:   false,
    72  		},
    73  		{
    74  			name:    "test5",
    75  			newArgs: []string{"wf", "-d"},
    76  			verbose: false,
    77  			time:    false,
    78  			dump:    true,
    79  			wfFile:  ".workflow.yaml",
    80  			rules:   false,
    81  		},
    82  		{
    83  			name:    "test6",
    84  			newArgs: []string{"wf", "-r"},
    85  			verbose: false,
    86  			time:    false,
    87  			dump:    false,
    88  			wfFile:  ".workflow.yaml",
    89  			rules:   true,
    90  		},
    91  		{
    92  			name:    "test7",
    93  			newArgs: []string{"wf", "-V", "-t", "-d", "-r", "-f", "TESTNAME"},
    94  			verbose: true,
    95  			time:    true,
    96  			dump:    true,
    97  			wfFile:  "TESTNAME",
    98  			rules:   true,
    99  		},
   100  	}
   101  	for _, tt := range tests {
   102  		t.Run(tt.name, func(t *testing.T) {
   103  			os.Args = tt.newArgs
   104  			got, got1, got2, got3, got4 := ParseArgs()
   105  			if !reflect.DeepEqual(*got, tt.verbose) {
   106  				t.Errorf("ParseArgs() got = %v, verbose %v", got, tt.verbose)
   107  			}
   108  			if !reflect.DeepEqual(*got1, tt.time) {
   109  				t.Errorf("ParseArgs() got1 = %v, verbose %v", got1, tt.time)
   110  			}
   111  			if !reflect.DeepEqual(*got2, tt.dump) {
   112  				t.Errorf("ParseArgs() got2 = %v, verbose %v", got2, tt.dump)
   113  			}
   114  			if !reflect.DeepEqual(*got3, tt.wfFile) {
   115  				t.Errorf("ParseArgs() got3 = %v, verbose %v", got3, tt.wfFile)
   116  			}
   117  			if !reflect.DeepEqual(*got4, tt.rules) {
   118  				t.Errorf("ParseArgs() got4 = %v, verbose %v", got4, tt.rules)
   119  			}
   120  
   121  			// to reset flag module so it can be reused
   122  			flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
   123  		})
   124  	}
   125  }
   126  
   127  func Test_dumpRulesFile(t *testing.T) {
   128  
   129  	dir, err := os.MkdirTemp("", "dumpRulesFile_test*")
   130  	if err != nil {
   131  		t.Fatalf("Unable to create tmp directory\n")
   132  	}
   133  	defer func() {
   134  		_ = os.RemoveAll(dir)
   135  	}()
   136  
   137  	oldPwd, _ := os.Getwd()
   138  	defer func() {
   139  		_ = os.Chdir(oldPwd)
   140  	}()
   141  
   142  	err = os.Chdir(dir)
   143  	if err != nil {
   144  		t.Fatalf("Unable to change directory to %s: %v\n", dir, err)
   145  	}
   146  
   147  	// make a .workflow.yaml
   148  	file := filepath.Join(dir, ".workflow.yaml")
   149  	if err = os.WriteFile(file, []byte("content"), 0600); err != nil {
   150  		t.Fatalf("Unable to create test .workflow.yaml")
   151  	}
   152  
   153  	type args struct {
   154  		f       string
   155  		verbose bool
   156  	}
   157  	tests := []struct {
   158  		name string
   159  		args args
   160  		want string
   161  	}{
   162  		{
   163  			name: "test",
   164  			args: args{
   165  				f:       ".workflow.yaml",
   166  				verbose: false,
   167  			},
   168  			want: "content\n",
   169  		},
   170  	}
   171  	for _, tt := range tests {
   172  		t.Run(tt.name, func(t *testing.T) {
   173  			r, w, _ := os.Pipe()
   174  			savedOut := os.Stdout
   175  			os.Stdout = w
   176  
   177  			dumpRulesFile(tt.args.f, tt.args.verbose)
   178  
   179  			outC := make(chan string)
   180  			go func() {
   181  				buf := bytes.Buffer{}
   182  				_, _ = io.Copy(&buf, r)
   183  				outC <- buf.String()
   184  			}()
   185  
   186  			_ = w.Close()
   187  			os.Stdout = savedOut
   188  			out := <-outC
   189  
   190  			if out != tt.want {
   191  				t.Errorf("dumpRulesFile() got1 = %v, verbose %v", out, tt.want)
   192  			}
   193  
   194  		})
   195  	}
   196  }
   197  
   198  func Test_printRules(t *testing.T) {
   199  	type args struct {
   200  		ourRcFile *rcparse.YRCfile
   201  	}
   202  
   203  	g := map[string]string{}
   204  	commands := map[string]rcparse.CmdEnv{}
   205  
   206  	commands["b"] = rcparse.CmdEnv{}
   207  	commands["a"] = rcparse.CmdEnv{}
   208  
   209  	x := rcparse.YRCfile{
   210  		G:        g,
   211  		Commands: commands,
   212  	}
   213  
   214  	tests := []struct {
   215  		name string
   216  		args args
   217  		want string
   218  	}{
   219  		{
   220  			name: "test",
   221  			args: args{&x},
   222  			want: "a\nb\n",
   223  		},
   224  	}
   225  	for _, tt := range tests {
   226  		t.Run(tt.name, func(t *testing.T) {
   227  
   228  			r, w, _ := os.Pipe()
   229  
   230  			savedOut := os.Stdout
   231  			os.Stdout = w
   232  			printRules(tt.args.ourRcFile)
   233  
   234  			outC := make(chan string)
   235  			go func() {
   236  				buf := bytes.Buffer{}
   237  				_, _ = io.Copy(&buf, r)
   238  				outC <- buf.String()
   239  			}()
   240  
   241  			_ = w.Close()
   242  			os.Stdout = savedOut
   243  			out := <-outC
   244  
   245  			if out != tt.want {
   246  				t.Errorf("printRules() got1 = %v, verbose %v", out, tt.want)
   247  			}
   248  
   249  		})
   250  	}
   251  }