github.com/claudiodangelis/banco@v0.0.0-20231219182139-c31d5d844fe5/module/bookmarks/bookmarks_test.go (about)

     1  package bookmarks
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  func setFakeHome() string {
    11  	wd, err := os.Getwd()
    12  	if err != nil {
    13  		panic(err)
    14  	}
    15  	fakehome := filepath.Join(filepath.Dir(filepath.Dir(wd)), "testdata", "config", "fakehome")
    16  	os.Setenv("HOME", fakehome)
    17  	return fakehome
    18  }
    19  
    20  func Test_getBrowserConfiguration(t *testing.T) {
    21  	fakehome := setFakeHome()
    22  	var NILSLICE []string
    23  	tests := []struct {
    24  		name               string
    25  		browserEnvVariable string
    26  		projectDir         string
    27  		wantCmd            string
    28  		wantArgs           []string
    29  	}{
    30  		{
    31  			"$BROWSER variable set, configuration file not overridden",
    32  			"chromium",
    33  			"myproject",
    34  			"chromium",
    35  			NILSLICE,
    36  		},
    37  		{
    38  			"$BROWSER variable set, configuration file overridden",
    39  			"chromium",
    40  			"myprojectcustomconfig",
    41  			"firefox",
    42  			[]string{"-p", "work"},
    43  		},
    44  	}
    45  	for _, tt := range tests {
    46  		wd, err := os.Getwd()
    47  		if err != nil {
    48  			panic(err)
    49  		}
    50  		if err := os.Chdir(filepath.Join(fakehome, tt.projectDir)); err != nil {
    51  			panic(err)
    52  		}
    53  		os.Setenv("BROWSER", tt.browserEnvVariable)
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			gotCmd, gotArgs := getBrowserConfiguration()
    56  			if gotCmd != tt.wantCmd {
    57  				t.Errorf("getBrowserConfiguration() gotCmd = %v, want %v", gotCmd, tt.wantCmd)
    58  			}
    59  			if !reflect.DeepEqual(gotArgs, tt.wantArgs) {
    60  				t.Errorf("getBrowserConfiguration() gotArgs = %v, want %v", gotArgs, tt.wantArgs)
    61  			}
    62  		})
    63  		os.Unsetenv("BROWSER")
    64  		if err := os.Chdir(wd); err != nil {
    65  			panic(err)
    66  		}
    67  	}
    68  }