github.com/ChicK00o/awgo@v0.29.4/testutils_test.go (about)

     1  // Copyright (c) 2018 Dean Jackson <deanishe@deanishe.net>
     2  // MIT Licence - http://opensource.org/licenses/MIT
     3  
     4  package aw
     5  
     6  import (
     7  	"fmt"
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"go.deanishe.net/env"
    15  )
    16  
    17  var (
    18  	tVersion                  = "1.2.0"
    19  	tName                     = "AwGo"
    20  	tBundleID                 = "net.deanishe.awgo"
    21  	tUID                      = "user.workflow.4B0E9731-E139-4179-BC50-D7FFF82B269A"
    22  	tDebug                    = true
    23  	tAlfredVersion            = "3.6"
    24  	tAlfredBuild              = "901"
    25  	tTheme                    = "alfred.theme.custom.DE3D17CA-64A2-4B42-A3F6-C71DB1201F88"
    26  	tThemeBackground          = "rgba(255,255,255,1.00)"
    27  	tThemeSelectionBackground = "rgba(255,255,255,1.00)"
    28  	tPreferences              = os.ExpandEnv("$HOME/Library/Application Support/Alfred/Alfred.alfredpreferences")
    29  	tLocalhash                = "0dd4500828b5c675862eaa7786cf0f374823b965"
    30  	tCacheDir                 = os.ExpandEnv("$HOME/Library/Caches/com.runningwithcrayons.Alfred/Workflow Data/net.deanishe.awgo")
    31  	tDataDir                  = os.ExpandEnv("$HOME/Library/Application Support/Alfred/Workflow Data/net.deanishe.awgo")
    32  
    33  	testEnv = env.MapEnv{
    34  		EnvVarVersion:          tVersion,
    35  		EnvVarName:             tName,
    36  		EnvVarBundleID:         tBundleID,
    37  		EnvVarUID:              tUID,
    38  		EnvVarDebug:            fmt.Sprintf("%v", tDebug),
    39  		EnvVarAlfredVersion:    tAlfredVersion,
    40  		EnvVarAlfredBuild:      tAlfredBuild,
    41  		EnvVarTheme:            tTheme,
    42  		EnvVarThemeBG:          tThemeBackground,
    43  		EnvVarThemeSelectionBG: tThemeSelectionBackground,
    44  		EnvVarPreferences:      tPreferences,
    45  		EnvVarLocalhash:        tLocalhash,
    46  		EnvVarCacheDir:         tCacheDir,
    47  		EnvVarDataDir:          tDataDir,
    48  	}
    49  )
    50  
    51  // Mock os.Exit
    52  type mockExit struct {
    53  	code int
    54  }
    55  
    56  func (me *mockExit) Exit(code int) { me.code = code }
    57  
    58  // Mock exec.Command
    59  type mockExec struct {
    60  	name string
    61  	args []string
    62  }
    63  
    64  func (me *mockExec) Run(name string, arg ...string) error {
    65  	me.name = name
    66  	me.args = append([]string{name}, arg...)
    67  	return nil
    68  }
    69  
    70  func panicOnErr(err error) {
    71  	if err != nil {
    72  		panic(err)
    73  	}
    74  }
    75  
    76  // create a temporary directory, call function fn, delete the directory.
    77  func withTempDir(fn func(dir string)) {
    78  	p, err := ioutil.TempDir("", "awgo-")
    79  	if err != nil {
    80  		panic(err)
    81  	}
    82  	if p, err = filepath.EvalSymlinks(p); err != nil {
    83  		panic(err)
    84  	}
    85  	defer panicOnErr(os.RemoveAll(p))
    86  	fn(p)
    87  }
    88  
    89  // Call function with a test environment.
    90  func withTestEnv(fn func(e env.MapEnv)) {
    91  	e := env.MapEnv{
    92  		EnvVarVersion:          tVersion,
    93  		EnvVarName:             tName,
    94  		EnvVarBundleID:         tBundleID,
    95  		EnvVarUID:              tUID,
    96  		EnvVarDebug:            fmt.Sprintf("%v", tDebug),
    97  		EnvVarAlfredVersion:    tAlfredVersion,
    98  		EnvVarAlfredBuild:      tAlfredBuild,
    99  		EnvVarTheme:            tTheme,
   100  		EnvVarThemeBG:          tThemeBackground,
   101  		EnvVarThemeSelectionBG: tThemeSelectionBackground,
   102  		EnvVarPreferences:      tPreferences,
   103  		EnvVarLocalhash:        tLocalhash,
   104  		EnvVarCacheDir:         tCacheDir,
   105  		EnvVarDataDir:          tDataDir,
   106  	}
   107  
   108  	fn(e)
   109  }
   110  
   111  // Call function in a test workflow environment.
   112  func withTestWf(fn func(wf *Workflow)) {
   113  	withTestEnv(func(e env.MapEnv) {
   114  		var (
   115  			dir string
   116  			err error
   117  		)
   118  
   119  		if dir, err = ioutil.TempDir("", "awgo-"); err != nil {
   120  			panic(err)
   121  		}
   122  		// TempDir() returns a symlink on my macOS :(
   123  		if dir, err = filepath.EvalSymlinks(dir); err != nil {
   124  			panic(err)
   125  		}
   126  
   127  		defer func() {
   128  			if err := os.RemoveAll(dir); err != nil {
   129  				panic(err)
   130  			}
   131  		}()
   132  
   133  		var (
   134  			dataDir  = filepath.Join(dir, "data")
   135  			cacheDir = filepath.Join(dir, "cache")
   136  		)
   137  
   138  		// Update env to point to cache & data dirs
   139  		e[EnvVarCacheDir] = cacheDir
   140  		e[EnvVarDataDir] = dataDir
   141  
   142  		// Create test files & directories
   143  		for _, p := range []string{dataDir, cacheDir} {
   144  			if err := os.MkdirAll(p, os.ModePerm); err != nil {
   145  				panic(err)
   146  			}
   147  		}
   148  
   149  		// Create workflow for current environment and pass it to function.
   150  		var wf = NewFromEnv(e)
   151  		fn(wf)
   152  	})
   153  }
   154  
   155  // TestWithTestWf verifies the withTestEnv helper.
   156  func TestWithTestWf(t *testing.T) {
   157  	withTestWf(func(wf *Workflow) {
   158  		wd, err := os.Getwd()
   159  		if err != nil {
   160  			t.Fatal(err)
   161  		}
   162  		data := []struct {
   163  			name, x, v string
   164  		}{
   165  			{"Workflow.Dir", wd, wf.Dir()},
   166  			{"Name", tName, wf.Name()},
   167  			{"BundleID", tBundleID, wf.BundleID()},
   168  
   169  			{"Config.UID", tUID, wf.Config.Get(EnvVarUID)},
   170  			{"Config.AlfredVersion", tAlfredVersion, wf.Config.Get(EnvVarAlfredVersion)},
   171  			{"Config.AlfredBuild", tAlfredBuild, wf.Config.Get(EnvVarAlfredBuild)},
   172  			{"Config.Theme", tTheme, wf.Config.Get(EnvVarTheme)},
   173  			{"Config.ThemeBackground", tThemeBackground, wf.Config.Get(EnvVarThemeBG)},
   174  			{"Config.ThemeSelectionBackground", tThemeSelectionBackground,
   175  				wf.Config.Get(EnvVarThemeSelectionBG)},
   176  			{"Config.Preferences", tPreferences, wf.Config.Get(EnvVarPreferences)},
   177  			{"Config.Localhash", tLocalhash, wf.Config.Get(EnvVarLocalhash)},
   178  		}
   179  
   180  		assert.Equal(t, tDebug, wf.Debug(), "unexpected debug")
   181  
   182  		for _, td := range data {
   183  			td := td
   184  			t.Run(td.name, func(t *testing.T) {
   185  				assert.Equal(t, td.x, td.v, "unexpected variable")
   186  			})
   187  		}
   188  	})
   189  }