@@ -80,6 +80,115 @@ run_entrypoint() {
8080 [[ " $output " == * " Input stack_name is required!" * ]]
8181}
8282
83+ # --- input whitespace (issue #1) ----------------------------------------------
84+
85+ # Source the script, run trim_inputs against a controlled environment, and
86+ # print the resulting value of one variable.
87+ #
88+ # The value is printed inside brackets on purpose: bats strips trailing
89+ # newlines from $output, so an untrimmed `host\n` would compare equal to `host`
90+ # and the assertion would pass whether the trimming happened or not.
91+ trim_and_print () {
92+ local var=" $1 "
93+ shift
94+ run env -i PATH=" ${PATH} " HOME=" ${BATS_TEST_TMPDIR} " WANT=" ${var} " " $@ " \
95+ bash -c "
96+ source '${ENTRYPOINT} '
97+ trim_inputs >/dev/null
98+ printf '[%s]' \"\$ {!WANT}\"
99+ "
100+ }
101+
102+ @test " remote_host loses a trailing newline (issue #1)" {
103+ trim_and_print REMOTE_HOST REMOTE_HOST=$' swarm.example.com\n '
104+ [ " $status " -eq 0 ]
105+ [ " $output " = " [swarm.example.com]" ]
106+ }
107+
108+ @test " remote_host loses whitespace in the middle of the value (issue #1)" {
109+ trim_and_print REMOTE_HOST REMOTE_HOST=' swarm .example.com'
110+ [ " $status " -eq 0 ]
111+ [ " $output " = " [swarm.example.com]" ]
112+ }
113+
114+ @test " remote_user is trimmed (issue #1)" {
115+ trim_and_print REMOTE_USER REMOTE_USER=$' deploy\n '
116+ [ " $status " -eq 0 ]
117+ [ " $output " = " [deploy]" ]
118+ }
119+
120+ @test " remote_port is trimmed (issue #1)" {
121+ trim_and_print REMOTE_PORT REMOTE_PORT=$' 2222\n '
122+ [ " $status " -eq 0 ]
123+ [ " $output " = " [2222]" ]
124+ }
125+
126+ @test " registry is trimmed (issue #1)" {
127+ trim_and_print REGISTRY REGISTRY=$' ghcr.io\n '
128+ [ " $status " -eq 0 ]
129+ [ " $output " = " [ghcr.io]" ]
130+ }
131+
132+ @test " username is trimmed (issue #1)" {
133+ trim_and_print USERNAME USERNAME=$' someone\n '
134+ [ " $status " -eq 0 ]
135+ [ " $output " = " [someone]" ]
136+ }
137+
138+ @test " trimming an input is reported on stdout (issue #1)" {
139+ # The point of the issue: the mistake is invisible everywhere else, so the
140+ # fix has to be visible without DEBUG.
141+ run env -i PATH=" ${PATH} " HOME=" ${BATS_TEST_TMPDIR} " \
142+ REMOTE_HOST=$' swarm.example.com\n ' \
143+ bash -c " source '${ENTRYPOINT} '; trim_inputs"
144+ [ " $status " -eq 0 ]
145+ [[ " $output " == * " Input remote_host: removed whitespace from the value" * ]]
146+ }
147+
148+ @test " a clean input is left alone and reported silently (issue #1)" {
149+ run env -i PATH=" ${PATH} " HOME=" ${BATS_TEST_TMPDIR} " \
150+ REMOTE_HOST=swarm.example.com REMOTE_USER=deploy REMOTE_PORT=22 \
151+ bash -c " source '${ENTRYPOINT} '; trim_inputs"
152+ [ " $status " -eq 0 ]
153+ [ -z " $output " ]
154+ }
155+
156+ @test " remote_private_key is never trimmed (issue #1)" {
157+ # Newlines are structural in a PEM key. Counting them is what makes a
158+ # regression here fail loudly instead of silently mangling the key.
159+ run env -i PATH=" ${PATH} " HOME=" ${BATS_TEST_TMPDIR} " \
160+ REMOTE_PRIVATE_KEY=$' -----BEGIN-----\n abc\n -----END-----\n ' \
161+ bash -c "
162+ source '${ENTRYPOINT} '
163+ trim_inputs >/dev/null
164+ printf '%s' \"\$ {REMOTE_PRIVATE_KEY}\" | wc -l
165+ "
166+ [ " $status " -eq 0 ]
167+ [ " ${output// / } " = " 3" ]
168+ }
169+
170+ @test " password is never trimmed (issue #1)" {
171+ trim_and_print PASSWORD PASSWORD=' tok en '
172+ [ " $status " -eq 0 ]
173+ [ " $output " = " [tok en ]" ]
174+ }
175+
176+ @test " the trim runs as part of the deploy flow (issue #1)" {
177+ # Every other test in this section calls trim_inputs directly; this one is
178+ # what proves it is actually wired into the script's flow, and that it runs
179+ # before the required-input checks.
180+ run_entrypoint REMOTE_HOST=$' swarm.example.com\n '
181+ [ " $status " -eq 1 ]
182+ [[ " $output " == * " Input remote_host: removed whitespace from the value" * ]]
183+ [[ " $output " == * " Input remote_user is required!" * ]]
184+ }
185+
186+ @test " a remote_host of only whitespace is reported as missing (issue #1)" {
187+ run_entrypoint REMOTE_HOST=$' \n '
188+ [ " $status " -eq 1 ]
189+ [[ " $output " == * " Input remote_host is required!" * ]]
190+ }
191+
83192# --- container registry login -------------------------------------------------
84193
85194@test " skips login when no credentials are given (issue #4)" {
0 commit comments