@@ -1768,6 +1768,40 @@ func TestWithStdout_SetsSpecifiedWriterAsStdout(t *testing.T) {
17681768 }
17691769}
17701770
1771+ func TestWithEnv_UnsetsAllEnvVarsGivenEmptySlice (t * testing.T ) {
1772+ t .Parallel ()
1773+ p := script .NewPipe ().WithEnv ([]string {"ENV1=test1" }).Exec ("sh -c 'echo ENV1=$ENV1'" )
1774+ want := "ENV1=test1\n "
1775+ got , err := p .String ()
1776+ if err != nil {
1777+ t .Fatal (err )
1778+ }
1779+ if got != want {
1780+ t .Fatalf ("want %q, got %q" , want , got )
1781+ }
1782+ got , err = p .Echo ("" ).WithEnv ([]string {}).Exec ("sh -c 'echo ENV1=$ENV1'" ).String ()
1783+ if err != nil {
1784+ t .Fatal (err )
1785+ }
1786+ want = "ENV1=\n "
1787+ if got != want {
1788+ t .Errorf ("want %q, got %q" , want , got )
1789+ }
1790+ }
1791+
1792+ func TestWithEnv_SetsGivenVariablesForSubsequentExec (t * testing.T ) {
1793+ t .Parallel ()
1794+ env := []string {"ENV1=test1" , "ENV2=test2" }
1795+ got , err := script .NewPipe ().WithEnv (env ).Exec ("sh -c 'echo ENV1=$ENV1 ENV2=$ENV2'" ).String ()
1796+ if err != nil {
1797+ t .Fatal (err )
1798+ }
1799+ want := "ENV1=test1 ENV2=test2\n "
1800+ if got != want {
1801+ t .Errorf ("want %q, got %q" , want , got )
1802+ }
1803+ }
1804+
17711805func TestErrorReturnsErrorSetByPreviousPipeStage (t * testing.T ) {
17721806 t .Parallel ()
17731807 p := script .File ("testdata/nonexistent.txt" )
@@ -1850,6 +1884,135 @@ func TestReadReturnsErrorGivenReadErrorOnPipe(t *testing.T) {
18501884 }
18511885}
18521886
1887+ func TestWait_ReturnsErrorPresentOnPipe (t * testing.T ) {
1888+ t .Parallel ()
1889+ p := script .Echo ("a\n b\n c\n " ).ExecForEach ("{{invalid template syntax}}" )
1890+ if p .Wait () == nil {
1891+ t .Error ("want error, got nil" )
1892+ }
1893+ }
1894+
1895+ func TestWait_DoesNotReturnErrorForValidExecution (t * testing.T ) {
1896+ t .Parallel ()
1897+ p := script .Echo ("a\n b\n c\n " ).ExecForEach ("echo \" {{.}}\" " )
1898+ if err := p .Wait (); err != nil {
1899+ t .Fatal (err )
1900+ }
1901+ }
1902+
1903+ var base64Cases = []struct {
1904+ name string
1905+ decoded string
1906+ encoded string
1907+ }{
1908+ {
1909+ name : "empty string" ,
1910+ decoded : "" ,
1911+ encoded : "" ,
1912+ },
1913+ {
1914+ name : "single line string" ,
1915+ decoded : "hello world" ,
1916+ encoded : "aGVsbG8gd29ybGQ=" ,
1917+ },
1918+ {
1919+ name : "multi line string" ,
1920+ decoded : "hello\n there\n world\n " ,
1921+ encoded : "aGVsbG8KdGhlcmUKd29ybGQK" ,
1922+ },
1923+ }
1924+
1925+ func TestEncodeBase64_CorrectlyEncodes (t * testing.T ) {
1926+ t .Parallel ()
1927+ for _ , tc := range base64Cases {
1928+ t .Run (tc .name , func (t * testing.T ) {
1929+ got , err := script .Echo (tc .decoded ).EncodeBase64 ().String ()
1930+ if err != nil {
1931+ t .Fatal (err )
1932+ }
1933+ if got != tc .encoded {
1934+ t .Logf ("input %q incorrectly encoded:" , tc .decoded )
1935+ t .Error (cmp .Diff (tc .encoded , got ))
1936+ }
1937+ })
1938+ }
1939+ }
1940+
1941+ func TestDecodeBase64_CorrectlyDecodes (t * testing.T ) {
1942+ t .Parallel ()
1943+ for _ , tc := range base64Cases {
1944+ t .Run (tc .name , func (t * testing.T ) {
1945+ got , err := script .Echo (tc .encoded ).DecodeBase64 ().String ()
1946+ if err != nil {
1947+ t .Fatal (err )
1948+ }
1949+ if got != tc .decoded {
1950+ t .Logf ("input %q incorrectly decoded:" , tc .encoded )
1951+ t .Error (cmp .Diff (tc .decoded , got ))
1952+ }
1953+ })
1954+ }
1955+ }
1956+
1957+ func TestEncodeBase64_FollowedByDecodeRecoversOriginal (t * testing.T ) {
1958+ t .Parallel ()
1959+ for _ , tc := range base64Cases {
1960+ t .Run (tc .name , func (t * testing.T ) {
1961+ decoded , err := script .Echo (tc .decoded ).EncodeBase64 ().DecodeBase64 ().String ()
1962+ if err != nil {
1963+ t .Fatal (err )
1964+ }
1965+ if decoded != tc .decoded {
1966+ t .Error ("encode-decode round trip failed:" , cmp .Diff (tc .decoded , decoded ))
1967+ }
1968+ encoded , err := script .Echo (tc .encoded ).DecodeBase64 ().EncodeBase64 ().String ()
1969+ if err != nil {
1970+ t .Fatal (err )
1971+ }
1972+ if encoded != tc .encoded {
1973+ t .Error ("decode-encode round trip failed:" , cmp .Diff (tc .encoded , encoded ))
1974+ }
1975+ })
1976+ }
1977+ }
1978+
1979+ func TestDecodeBase64_CorrectlyDecodesInputToBytes (t * testing.T ) {
1980+ t .Parallel ()
1981+ input := "CAAAEA=="
1982+ got , err := script .Echo (input ).DecodeBase64 ().Bytes ()
1983+ if err != nil {
1984+ t .Fatal (err )
1985+ }
1986+ want := []byte {8 , 0 , 0 , 16 }
1987+ if ! bytes .Equal (want , got ) {
1988+ t .Logf ("input %#v incorrectly decoded:" , input )
1989+ t .Error (cmp .Diff (want , got ))
1990+ }
1991+ }
1992+
1993+ func TestEncodeBase64_CorrectlyEncodesInputBytes (t * testing.T ) {
1994+ t .Parallel ()
1995+ input := []byte {8 , 0 , 0 , 16 }
1996+ reader := bytes .NewReader (input )
1997+ want := "CAAAEA=="
1998+ got , err := script .NewPipe ().WithReader (reader ).EncodeBase64 ().String ()
1999+ if err != nil {
2000+ t .Fatal (err )
2001+ }
2002+ if got != want {
2003+ t .Logf ("input %#v incorrectly encoded:" , input )
2004+ t .Error (cmp .Diff (want , got ))
2005+ }
2006+ }
2007+
2008+ func TestWithStdErr_IsConcurrencySafeAfterExec (t * testing.T ) {
2009+ t .Parallel ()
2010+ err := script .Exec ("echo" ).WithStderr (nil ).Wait ()
2011+ if err != nil {
2012+ t .Fatal (err )
2013+ }
2014+ }
2015+
18532016func ExampleArgs () {
18542017 script .Args ().Stdout ()
18552018 // prints command-line arguments
@@ -1969,6 +2132,12 @@ func ExamplePipe_CountLines() {
19692132 // 3
19702133}
19712134
2135+ func ExamplePipe_DecodeBase64 () {
2136+ script .Echo ("SGVsbG8sIHdvcmxkIQ==" ).DecodeBase64 ().Stdout ()
2137+ // Output:
2138+ // Hello, world!
2139+ }
2140+
19722141func ExamplePipe_Do () {
19732142 ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
19742143 data , err := io .ReadAll (r .Body )
@@ -2004,6 +2173,12 @@ func ExamplePipe_Echo() {
20042173 // Hello, world!
20052174}
20062175
2176+ func ExamplePipe_EncodeBase64 () {
2177+ script .Echo ("Hello, world!" ).EncodeBase64 ().Stdout ()
2178+ // Output:
2179+ // SGVsbG8sIHdvcmxkIQ==
2180+ }
2181+
20072182func ExamplePipe_ExitStatus () {
20082183 p := script .Exec ("echo" )
20092184 fmt .Println (p .ExitStatus ())
0 commit comments