github.com/clysto/awgo@v0.15.0/testutils_test.go (about)

     1  //
     2  // Copyright (c) 2018 Dean Jackson <deanishe@deanishe.net>
     3  //
     4  // MIT Licence. See http://opensource.org/licenses/MIT
     5  //
     6  // Created on 2018-02-09
     7  //
     8  
     9  package aw
    10  
    11  import (
    12  	"fmt"
    13  	"io/ioutil"
    14  	"os"
    15  	"path/filepath"
    16  	"testing"
    17  )
    18  
    19  var (
    20  	tVersion                  = "0.14"
    21  	tName                     = "AwGo"
    22  	tBundleID                 = "net.deanishe.awgo"
    23  	tUID                      = "user.workflow.4B0E9731-E139-4179-BC50-D7FFF82B269A"
    24  	tDebug                    = true
    25  	tAlfredVersion            = "3.6"
    26  	tAlfredBuild              = "901"
    27  	tTheme                    = "alfred.theme.custom.DE3D17CA-64A2-4B42-A3F6-C71DB1201F88"
    28  	tThemeBackground          = "rgba(255,255,255,1.00)"
    29  	tThemeSelectionBackground = "rgba(255,255,255,1.00)"
    30  	tPreferences              = os.ExpandEnv("$HOME/Library/Application Support/Alfred 3/Alfred.alfredpreferences")
    31  	tLocalhash                = "0dd4500828b5c675862eaa7786cf0f374823b965"
    32  	tCacheDir                 = os.ExpandEnv("$HOME/Library/Caches/com.runningwithcrayons.Alfred-3/Workflow Data/net.deanishe.awgo")
    33  	tDataDir                  = os.ExpandEnv("$HOME/Library/Application Support/Alfred 3/Workflow Data/net.deanishe.awgo")
    34  
    35  	testEnv = MapEnv{
    36  		EnvVarVersion:          tVersion,
    37  		EnvVarName:             tName,
    38  		EnvVarBundleID:         tBundleID,
    39  		EnvVarUID:              tUID,
    40  		EnvVarDebug:            fmt.Sprintf("%v", tDebug),
    41  		EnvVarAlfredVersion:    tAlfredVersion,
    42  		EnvVarAlfredBuild:      tAlfredBuild,
    43  		EnvVarTheme:            tTheme,
    44  		EnvVarThemeBG:          tThemeBackground,
    45  		EnvVarThemeSelectionBG: tThemeSelectionBackground,
    46  		EnvVarPreferences:      tPreferences,
    47  		EnvVarLocalhash:        tLocalhash,
    48  		EnvVarCacheDir:         tCacheDir,
    49  		EnvVarDataDir:          tDataDir,
    50  	}
    51  
    52  	tInfoPlist = `<?xml version="1.0" encoding="UTF-8"?>
    53  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    54  <plist version="1.0">
    55  <dict>
    56  	<key>bundleid</key>
    57  	<string>net.deanishe.awgo</string>
    58  	<key>connections</key>
    59  	<dict/>
    60  	<key>createdby</key>
    61  	<string>Dean Jackson</string>
    62  	<key>description</key>
    63  	<string>AwGo sample info.plist</string>
    64  	<key>disabled</key>
    65  	<false/>
    66  	<key>name</key>
    67  	<string>AwGo</string>
    68  	<key>objects</key>
    69  	<array/>
    70  	<key>readme</key>
    71  	<string></string>
    72  	<key>uidata</key>
    73  	<dict/>
    74  	<key>webaddress</key>
    75  	<string>https://github.com/deanishe/awgo</string>
    76      <key>version</key>
    77      <string>0.14</string>
    78  	<key>variables</key>
    79  	<dict>
    80  		<key>exported_var</key>
    81  		<string>exported_value</string>
    82  		<key>unexported_var</key>
    83  		<string>unexported_value</string>
    84  	</dict>
    85  	<key>variablesdontexport</key>
    86  	<array>
    87  		<string>unexported_var</string>
    88  	</array>
    89  </dict>
    90  </plist>
    91  `
    92  )
    93  
    94  // Call function with a test environment.
    95  func withTestEnv(fun func(e MapEnv)) {
    96  	e := MapEnv{
    97  		EnvVarVersion:          tVersion,
    98  		EnvVarName:             tName,
    99  		EnvVarBundleID:         tBundleID,
   100  		EnvVarUID:              tUID,
   101  		EnvVarDebug:            fmt.Sprintf("%v", tDebug),
   102  		EnvVarAlfredVersion:    tAlfredVersion,
   103  		EnvVarAlfredBuild:      tAlfredBuild,
   104  		EnvVarTheme:            tTheme,
   105  		EnvVarThemeBG:          tThemeBackground,
   106  		EnvVarThemeSelectionBG: tThemeSelectionBackground,
   107  		EnvVarPreferences:      tPreferences,
   108  		EnvVarLocalhash:        tLocalhash,
   109  		EnvVarCacheDir:         tCacheDir,
   110  		EnvVarDataDir:          tDataDir,
   111  	}
   112  
   113  	fun(e)
   114  }
   115  
   116  // Call function in a test workflow environment.
   117  func withTestWf(fun func(wf *Workflow)) {
   118  
   119  	withTestEnv(func(e MapEnv) {
   120  
   121  		var (
   122  			curdir, dir string
   123  			err         error
   124  		)
   125  
   126  		curdir, err = os.Getwd()
   127  		if err != nil {
   128  			panic(err)
   129  		}
   130  
   131  		dir, err = ioutil.TempDir("", "awgo-")
   132  		if err != nil {
   133  			panic(err)
   134  		}
   135  
   136  		defer func() {
   137  			if err := os.RemoveAll(dir); err != nil {
   138  				panic(err)
   139  			}
   140  		}()
   141  
   142  		// TempDir() returns a symlink on my macOS :(
   143  		dir, err = filepath.EvalSymlinks(dir)
   144  		if err != nil {
   145  			panic(err)
   146  		}
   147  
   148  		var (
   149  			wfdir    = filepath.Join(dir, "workflow")
   150  			datadir  = filepath.Join(dir, "data")
   151  			cachedir = filepath.Join(dir, "cache")
   152  			ipfile   = filepath.Join(wfdir, "info.plist")
   153  		)
   154  
   155  		// Update env to point to cache & data dirs
   156  		e[EnvVarCacheDir] = cachedir
   157  		e[EnvVarDataDir] = datadir
   158  
   159  		// Create test files & directories
   160  		for _, p := range []string{wfdir, datadir, cachedir} {
   161  			if err := os.MkdirAll(p, os.ModePerm); err != nil {
   162  				panic(err)
   163  			}
   164  		}
   165  		// info.plist
   166  		if err := ioutil.WriteFile(ipfile, []byte(tInfoPlist), os.ModePerm); err != nil {
   167  			panic(err)
   168  		}
   169  
   170  		// Change to workflow directory and call function from there.
   171  		if err := os.Chdir(wfdir); err != nil {
   172  			panic(err)
   173  		}
   174  
   175  		defer func() {
   176  			if err := os.Chdir(curdir); err != nil {
   177  				panic(err)
   178  			}
   179  		}()
   180  
   181  		// Create workflow for current environment and pass it to function.
   182  		var wf = NewFromEnv(e)
   183  		fun(wf)
   184  	})
   185  
   186  }
   187  
   188  // TestWithTestWf verifies the withTestEnv helper.
   189  func TestWithTestWf(t *testing.T) {
   190  
   191  	withTestWf(func(wf *Workflow) {
   192  
   193  		wd, err := os.Getwd()
   194  		if err != nil {
   195  			t.Fatal(err)
   196  		}
   197  		cd := filepath.Join(wd, "../cache")
   198  		dd := filepath.Join(wd, "../data")
   199  
   200  		data := []struct {
   201  			name, x, v string
   202  		}{
   203  			{"Workflow.Dir", wd, wf.Dir()},
   204  			{"Cache.Dir", cd, wf.Cache.Dir},
   205  			{"CacheDir", cd, wf.CacheDir()},
   206  			{"Data.Dir", dd, wf.Data.Dir},
   207  			{"DataDir", dd, wf.DataDir()},
   208  			{"Name", tName, wf.Name()},
   209  			{"BundleID", tBundleID, wf.BundleID()},
   210  
   211  			{"Config.UID", tUID, wf.Config.Get(EnvVarUID)},
   212  			{"Config.AlfredVersion", tAlfredVersion, wf.Config.Get(EnvVarAlfredVersion)},
   213  			{"Config.AlfredBuild", tAlfredBuild, wf.Config.Get(EnvVarAlfredBuild)},
   214  			{"Config.Theme", tTheme, wf.Config.Get(EnvVarTheme)},
   215  			{"Config.ThemeBackground", tThemeBackground, wf.Config.Get(EnvVarThemeBG)},
   216  			{"Config.ThemeSelectionBackground", tThemeSelectionBackground,
   217  				wf.Config.Get(EnvVarThemeSelectionBG)},
   218  			{"Config.Preferences", tPreferences, wf.Config.Get(EnvVarPreferences)},
   219  			{"Config.Localhash", tLocalhash, wf.Config.Get(EnvVarLocalhash)},
   220  			{"Config.CacheDir", cd, wf.Config.Get(EnvVarCacheDir)},
   221  			{"Config.DataDir", dd, wf.Config.Get(EnvVarDataDir)},
   222  		}
   223  
   224  		if wf.Debug() != tDebug {
   225  			t.Errorf("Bad Debug(). Expected=%v, Got=%v", tDebug, wf.Debug())
   226  		}
   227  
   228  		for _, td := range data {
   229  			if td.v != td.x {
   230  				t.Errorf("Bad %s. Expected=%#v, Got=%#v", td.name, td.x, td.v)
   231  			}
   232  		}
   233  
   234  	})
   235  }
   236  
   237  // slicesEqual tests if 2 string slices are equal.
   238  func slicesEqual(a, b []string) bool {
   239  
   240  	if a == nil && b == nil {
   241  		return true
   242  	}
   243  
   244  	if a == nil || b == nil {
   245  		return false
   246  	}
   247  
   248  	if len(a) != len(b) {
   249  		return false
   250  	}
   251  
   252  	for i := range a {
   253  		if a[i] != b[i] {
   254  			return false
   255  		}
   256  	}
   257  
   258  	return true
   259  }
   260  
   261  // tests whether two maps have the same contents.
   262  func verifyMapsEqual(a, b map[string]string) error {
   263  
   264  	if a == nil && b == nil {
   265  		return nil
   266  	}
   267  
   268  	if a == nil || b == nil {
   269  		return fmt.Errorf("nil map. a=%v, b=%v", a, b)
   270  	}
   271  
   272  	if len(a) != len(b) {
   273  		return fmt.Errorf("different lengths (%d != %d)", len(a), len(b))
   274  	}
   275  
   276  	for k, v := range a {
   277  
   278  		v2, ok := b[k]
   279  		if !ok {
   280  			return fmt.Errorf("key %v missing in b", k)
   281  
   282  		} else if v2 != v {
   283  			return fmt.Errorf("%s is different: %#v != %#v", k, v, v2)
   284  		}
   285  	}
   286  
   287  	return nil
   288  }