| 1 |
// Copyright (c) 2025, Ville Skyttä <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
package fileutil |
| 5 |
|
| 6 |
import ( |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func TestShebang(t *testing.T) { |
| 12 |
t.Parallel() |
| 13 |
tests := []struct { |
| 14 |
in []byte |
| 15 |
want string |
| 16 |
}{ |
| 17 |
{ |
| 18 |
in: []byte("#!/usr/bin/env bash"), |
| 19 |
want: "bash", |
| 20 |
}, |
| 21 |
{ |
| 22 |
in: []byte("#!/bin/bash"), |
| 23 |
want: "bash", |
| 24 |
}, |
| 25 |
{ |
| 26 |
in: []byte("#!foo bar"), |
| 27 |
want: "", |
| 28 |
}, |
| 29 |
{ |
| 30 |
in: []byte("#!/bin/dash"), |
| 31 |
want: "dash", |
| 32 |
}, |
| 33 |
{ |
| 34 |
in: []byte("#!/usr/bin/env dash"), |
| 35 |
want: "dash", |
| 36 |
}, |
| 37 |
{ |
| 38 |
in: []byte("#!/bin/zsh"), |
| 39 |
want: "zsh", |
| 40 |
}, |
| 41 |
{ |
| 42 |
in: []byte("#! /bin/zsh true"), |
| 43 |
want: "zsh", |
| 44 |
}, |
| 45 |
{ |
| 46 |
in: []byte("#! /bin/zsh"), |
| 47 |
want: "zsh", |
| 48 |
}, |
| 49 |
{ |
| 50 |
in: []byte("#!\t/bin/zsh"), |
| 51 |
want: "zsh", |
| 52 |
}, |
| 53 |
{ |
| 54 |
in: []byte("#!\f/bin/zsh"), |
| 55 |
want: "", |
| 56 |
}, |
| 57 |
} |
| 58 |
|
| 59 |
for _, test := range tests { |
| 60 |
name := strings.ReplaceAll(strings.ReplaceAll(string(test.in), "\f", "\\f"), "\t", "\\t") |
| 61 |
t.Run(name, func(t *testing.T) { |
| 62 |
if got := Shebang(test.in); got != test.want { |
| 63 |
t.Fatalf("want %q, got %q", test.want, got) |
| 64 |
} |
| 65 |
}) |
| 66 |
} |
| 67 |
} |