]> Sergey Matveev's repositories - zsh-autoenv.git/blob - tests/recurse-upwards.t
Fix $PWD while sourcing .env file - should be dir of .env file
[zsh-autoenv.git] / tests / recurse-upwards.t
1 Test recursing into parent .env files.
2
3   $ source $TESTDIR/setup.sh
4
5 Setup env actions / output.
6
7   $ AUTOENV_LOOK_UPWARDS=1
8
9 Create env files in root dir.
10
11   $ echo 'echo ENTERED_root: PWD:${PWD:t} from:${_autoenv_from_dir:t} to:${_autoenv_to_dir:t}' > .env
12   $ echo 'echo LEFT_root: PWD:${PWD:t} from:${_autoenv_from_dir:t} to:${_autoenv_to_dir:t}' > .env.leave
13   $ test_autoenv_auth_env_files
14
15 Create env files in sub dir.
16
17   $ mkdir -p sub/sub2
18   $ cd sub
19   ENTERED_root: PWD:recurse-upwards.t from:recurse-upwards.t to:sub
20
21   $ echo 'echo ENTERED_sub: PWD:${PWD:t} from:${_autoenv_from_dir:t} to:${_autoenv_to_dir:t}' > .env
22   $ echo 'echo LEFT_sub: PWD:${PWD:t} from:${_autoenv_from_dir:t} to:${_autoenv_to_dir:t}' > .env.leave
23   $ test_autoenv_auth_env_files
24
25 The actual tests.
26
27   $ cd .
28   ENTERED_sub: PWD:sub from:sub to:sub
29
30   $ cd ..
31   LEFT_sub: PWD:sub from:sub to:recurse-upwards.t
32
33   $ cd sub/sub2
34   ENTERED_sub: PWD:sub from:recurse-upwards.t to:sub2
35
36   $ cd ..
37
38 Changing the .env file should re-source it.
39
40   $ echo 'echo ENTER2' >> .env
41
42 Set timestamp of auth file into the past, so it gets seen as new below.
43
44   $ touch -t 201401010101 .env
45
46   $ test_autoenv_auth_env_files
47   $ cd .
48   ENTERED_sub: PWD:sub from:sub to:sub
49   ENTER2
50
51 Add sub/sub2/.env file, with a call to autoenv_source_parent.
52
53   $ echo "echo autoenv_source_parent_from_sub2:\nautoenv_source_parent\necho done_sub2\n" > sub2/.env
54   $ test_autoenv_add_to_env sub2/.env
55   $ cd sub2
56   autoenv_source_parent_from_sub2:
57   ENTERED_sub: PWD:sub from:sub to:sub2
58   ENTER2
59   done_sub2
60
61 Move sub/.env away, now the root .env file should get sourced.
62
63   $ mv ../.env ../.env.out
64   $ touch -t 201401010102 .env
65   $ cd .
66   autoenv_source_parent_from_sub2:
67   ENTERED_root: PWD:recurse-upwards.t from:sub2 to:sub2
68   done_sub2
69   $ mv ../.env.out ../.env
70
71 Prepend call to autoenv_source_parent to sub/.env file.
72
73   $ cd ..
74   $ sed -i -e "1s/^/echo autoenv_source_parent_from_sub:\nautoenv_source_parent\n/" .env
75   $ echo "echo done_sub" >> .env
76   $ touch -t 201401010103 .env
77   $ test_autoenv_auth_env_files
78
79   $ cd .
80   autoenv_source_parent_from_sub:
81   ENTERED_root: PWD:recurse-upwards.t from:sub to:sub
82   ENTERED_sub: PWD:sub from:sub to:sub
83   ENTER2
84   done_sub
85
86
87 Add sub/sub2/.env file.
88
89   $ echo -e "echo autoenv_source_parent_from_sub2:\nautoenv_source_parent\necho done_sub2\n" > sub2/.env
90   $ test_autoenv_add_to_env sub2/.env
91   $ cd sub2
92   autoenv_source_parent_from_sub2:
93   autoenv_source_parent_from_sub:
94   ENTERED_root: PWD:recurse-upwards.t from:sub to:sub
95   ENTERED_sub: PWD:sub from:sub to:sub
96   ENTER2
97   done_sub
98   done_sub2
99
100 Go to root.
101 This should not trigger the enter event, because it was handled via
102 autoenv_source_parent already.
103
104   $ cd ../..
105   LEFT_sub: PWD:sub from:sub2 to:recurse-upwards.t
106
107
108 Changing the root .env should trigger re-authentication via autoenv_source_parent.
109
110 First, let's answer "no".
111
112   $ echo "echo NEW" > .env
113   $ _autoenv_read_answer() { echo 'n' }
114   $ cd sub
115   autoenv_source_parent_from_sub:
116   Attempting to load unauthorized env file: /tmp/cramtests-*/recurse-upwards.t/.env (glob)
117   
118   **********************************************
119   
120   echo NEW
121   
122   **********************************************
123   
124   Would you like to authorize it? [y/N] 
125   ENTERED_sub: PWD:sub from:recurse-upwards.t to:sub
126   ENTER2
127   done_sub
128
129 Now with "yes".
130 This currently does not trigger re-execution of the .env file.
131
132   $ _autoenv_read_answer() { echo 'y' }
133   $ cd .
134
135 Touching the .env file will now source the parent env file.
136
137   $ touch -t 201401010104 .env
138   $ cd .
139   autoenv_source_parent_from_sub:
140   Attempting to load unauthorized env file: /tmp/cramtests-*/recurse-upwards.t/.env (glob)
141   
142   **********************************************
143   
144   echo NEW
145   
146   **********************************************
147   
148   Would you like to authorize it? [y/N] 
149   NEW
150   ENTERED_sub: PWD:sub from:sub to:sub
151   ENTER2
152   done_sub
153
154
155   $ cd ..
156   LEFT_sub: PWD:sub from:sub to:recurse-upwards.t
157   $ mkdir sub/sub2/sub3
158   $ cd sub/sub2/sub3
159   autoenv_source_parent_from_sub2:
160   autoenv_source_parent_from_sub:
161   NEW
162   ENTERED_sub: PWD:sub from:recurse-upwards.t to:sub
163   ENTER2
164   done_sub
165   done_sub2