Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
For stubbing CLI tools to test your wild and crazy shell scripts
It runs the shell script that you want to test via child_process.exec. Before it does that, it takes a bunch of stub scripts that you've written and places them in a temporary directory. It adds that directory to the $PATH
that the child process will have access to. So, your stub scripts get called instead of the real CLI tools.
It also gives you a way to make assertions from within your stubbed scripts about the expected arguments that the function was called with.
Say that the script I want to test is a file called download-latest.sh
and looks like this:
#!/usr/bin/env bash
bucket=${1}
prefix=${2}
destination=${3}
latest=$(aws s3 cp s3://${bucket}/${prefix}/latest -)
aws s3 cp s3://${bucket}/${prefix}/${latest} ${destination}
When I test this script, I am confident that as long as I provide aws
with the right arguments, it will do what I expect it to do. However
I don't want to have to reach out an talk to real-life S3 every time I run the test. So I can write a test like this:
var test = require('tape');
var barsh = require('barsh');
var fs = require('fs');
test('[my script] downloads the latest file', function(assert) {
var testBucket = 'my-bucket';
var testPrefix = 'some-prefix';
var fixture = './fixtures/file';
var destination = './file-received';
var stubs = {};
stubs.aws = `
#!/usr/bin/env bash
assert equal $1 s3 "calls aws s3"
assert equal $2 cp "calls aws s3 cp"
if [[ "$3" == *latest ]]; then
assert equal $3 s3://${testBucket}/${testPrefix}/latest "request the correct latest file"
assert equal $4 - "pipes latest file to stdout"
echo "contents-of-latest-file"
else
assert equal $3 s3://${testBucket}/${testPrefix}/contents-of-latest-file "downloads the correct file"
cp ${fixture} $4
fi
`;
var command = `../scripts/download-latest.sh ${testBucket} ${testPrefix} ${destination}`;
barsh(assert).exec(command, stubs, function(err, stdout, stderr) {
assert.ifError(err, 'completed without error');
var downloaded = fs.readFileSync(destination, 'utf8');
var expected = fs.readFileSync(fixture, 'utf8');
assert.equal(downloaded, expected, 'downloaded latest file');
assert.end();
});
});
Running this example test will give a console output like this:
TAP version 13
# [my script] downloads the latest file
ok 1 calls aws s3
ok 2 calls aws s3 cp
ok 3 request the correct latest file
ok 4 pipes latest file to stdout
ok 5 calls aws s3
ok 6 calls aws s3 cp
ok 7 downloads the correct file
ok 8 completed without error
ok 9 downloaded latest file
1..9
# tests 9
# pass 9
# ok
FAQs
For stubbing CLI commands to test your wild and crazy shell scripts
We found that barsh demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.