-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Torsten Long <[email protected]>
- Loading branch information
Showing
10 changed files
with
399 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Always ignore the sumfile because users won't have one either. | ||
go.sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) 2022 - for information on the respective copyright owner | ||
// see the NOTICE file or the repository | ||
// https://github.com/boschresearch/shellmock | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
|
||
module main | ||
|
||
go 1.22.1 | ||
|
||
require ( | ||
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 | ||
mvdan.cc/sh/v3 v3.8.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) 2022 - for information on the respective copyright owner | ||
// see the NOTICE file or the repository | ||
// https://github.com/boschresearch/shellmock | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
|
||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"slices" | ||
|
||
"golang.org/x/exp/maps" | ||
shell "mvdan.cc/sh/v3/syntax" | ||
) | ||
|
||
// Determine all commands executed by a script. | ||
func findCommands(shellCode shell.Node) map[string]int { | ||
result := map[string]int{} | ||
|
||
shell.Walk( | ||
shellCode, func(node shell.Node) bool { | ||
// Simple commands. | ||
if expr, ok := node.(*shell.CallExpr); ok { | ||
if len(expr.Args) == 0 || len(expr.Args[0].Parts) == 0 { | ||
// Ignore empty commands and continue searching. | ||
return true | ||
} | ||
// We do not detect cases where a command is an argument. We also do not detect | ||
// cases where the command we seek is hidden in a command substitution or shell | ||
// expansion. | ||
if cmd, ok := expr.Args[0].Parts[0].(*shell.Lit); ok { | ||
result[cmd.Value]++ | ||
} | ||
} | ||
// Continue searching. | ||
return true | ||
}, | ||
) | ||
|
||
return result | ||
} | ||
|
||
func main() { | ||
content, err := io.ReadAll(bufio.NewReader(os.Stdin)) | ||
if err != nil { | ||
log.Fatalf("failed to read from stdin: %s", err.Error()) | ||
} | ||
parsed, err := shell.NewParser().Parse(bytes.NewReader(content), "") | ||
if err != nil { | ||
log.Fatalf("failed to parse shell code: %s", err.Error()) | ||
} | ||
commands := findCommands(parsed) | ||
keys := maps.Keys(commands) | ||
slices.Sort(keys) | ||
for _, cmd := range keys { | ||
count := commands[cmd] | ||
fmt.Printf("%s:%d\n", cmd, count) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/bin/bash | ||
|
||
# Copyright (c) 2022 - for information on the respective copyright owner | ||
# see the NOTICE file or the repository | ||
# https://github.com/boschresearch/shellmock | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
# use this file except in compliance with the License. You may obtain a copy of | ||
# the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations under | ||
# the License. | ||
|
||
# This file contains functionality needed for the command used to check which | ||
# executables a script is executing. That makes it easier to determine which | ||
# ones to mock. | ||
__shellmock__commands() { | ||
__shellmock_internal_pathcheck | ||
__shellmock_internal_trapcheck | ||
|
||
local check_functions=0 | ||
local usage_counts=0 | ||
while [[ $# -gt 0 ]]; do | ||
case $1 in | ||
--check-functions | -f) | ||
local check_functions=1 | ||
;; | ||
--usage-counts | -c) | ||
local usage_counts=1 | ||
;; | ||
*) | ||
echo >&2 "Unknown argument '$1'." | ||
return 1 | ||
;; | ||
esac | ||
shift | ||
done | ||
|
||
if ! command -v go &> /dev/null; then | ||
echo >&2 "The 'commands' command requires a Go toolchain." \ | ||
"Get it from here: https://go.dev/doc/install" | ||
return 1 | ||
fi | ||
|
||
if [[ -t 0 ]]; then | ||
echo >&2 "Shell code is read from stdin but stdin is a terminal, aborting." | ||
return 1 | ||
fi | ||
local code | ||
code="$(cat -)" | ||
|
||
# Build the binary used to analyse the shell code. | ||
local bin="${__SHELLMOCK_GO_MOD}/main" | ||
if ! [[ -x ${bin} ]]; then | ||
__shellmock_internal_init_command_search "${__SHELLMOCK_GO_MOD}" | ||
(cd "${__SHELLMOCK_GO_MOD}" && go get && go build) 1>&2 | ||
fi | ||
|
||
declare -A builtins | ||
local tmp | ||
while read -r tmp; do | ||
builtins["${tmp}"]=1 | ||
done < <(compgen -b) && wait $! || return 1 | ||
|
||
local cmd | ||
while read -r tmp; do | ||
cmd="${tmp%:*}" | ||
# Only output if it is neither a currently defined function or a built-in. | ||
if | ||
[[ -z ${builtins["${cmd}"]-} ]] \ | ||
&& [[ ${check_functions} -eq 1 || | ||
$(type -t "${cmd}" || :) != function ]] | ||
then | ||
# Adjust output format as requested. | ||
if [[ ${usage_counts} -eq 1 ]]; then | ||
echo "${tmp}" | ||
else | ||
echo "${cmd}" | ||
fi | ||
fi | ||
done < <("${bin}" <<< "${code}") && wait $! || return 1 | ||
} |
Oops, something went wrong.