Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test driver for srb snapshot tests #564

Merged
merged 8 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .buildkite/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ steps:
command: .buildkite/release-static.sh
agents:
os: linux
- wait: ~
# TODO(jez) Test on linux
- label: "Test: sorbet gem on Mac"
command: .buildkite/test-srb.sh
agents:
os: mac
- wait: ~
- label: "Deploy"
command: .buildkite/publish.sh
Expand Down
36 changes: 36 additions & 0 deletions .buildkite/test-srb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

# Using set -u doesn't work well with rbenv
set -eo pipefail

echo "--- Pre-setup"

unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) platform="linux";;
Darwin*) platform="mac";;
*) exit 1
esac

buildkite-agent artifact download "_out_/$platform/sorbet" .

mkdir -p gems/sorbet-static/libexec
mv "_out_/$platform/sorbet" gems/sorbet-static/libexec
chmod +x gems/sorbet-static/libexec/sorbet

echo "--- setup :ruby:"

pushd gems/sorbet

# rbenv init has uninitialized variables
eval "$(rbenv init -)"

rbenv shell 2.4.3

echo "+++ tests"

# TODO(jez) test/snapshot/driver.sh is not currently capable of testing the actual gem.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you going to fix this? If not, I'd not bother with the TODO

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I plan to fix this in literally the next PR I write. I want to keep the TODO there because it helps me remember which things are in progress.

# This currently tests that local development isn't broken.
test/snapshot/driver.sh

popd
2 changes: 1 addition & 1 deletion gems/sorbet/Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

task :test do
Dir.glob('./test/**/*.rb').each(&method(:require))
Dir.glob('./test/**/*.rb').reject {|f| f =~ /^\.\/test\/snapshot/}.each(&method(:require))
end
16 changes: 12 additions & 4 deletions gems/sorbet/bin/srb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ EOF
subcommand=$1
shift

# /path/to/gems/sorbet-0.0.1/bin/srb
srb_path="${BASH_SOURCE[0]}"

typecheck() {
# We're using bash string operations here to avoid forking.
# Using dirname / basename / etc. would mean ~15ms for each call.

# /path/to/gems/sorbet-0.0.1/bin/srb
srb_path="${BASH_SOURCE[0]}"
# /path/to/gems/sorbet-0.0.1
without_bin_srb="${srb_path%/bin/srb}"
# -0.0.1
Expand All @@ -47,13 +48,20 @@ typecheck() {
"${sorbet[0]}" "$@"
}

# Dynamically computing the path to the srb-rbi script lets this script work
# the same way in local development and when packaged as a gem. Either:
#
# /path/to/gems/sorbet-0.0.1/bin/srb-rbi
# path/to/bin/srb-rbi
srb_rbi_path="$srb_path-rbi"

case $subcommand in
"initialize" | "init")
bundle exec srb-rbi
"$srb_rbi_path"
;;

"rbi")
bundle exec srb-rbi "$@"
"$srb_rbi_path" "$@"
;;

"" | "typecheck" | "tc" | "t")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def initialize(files:, delegate_classes:)
def serialize(output_dir)
gem_class_defs = preprocess(@files)

FileUtils.mkdir_p(output_dir)
FileUtils.mkdir_p(output_dir) unless gem_class_defs.empty?

gem_class_defs.each do |gem, klass_ids|
File.open("#{File.join(output_dir, gem[:gem])}.rbi", 'w') do |f|
Expand Down Expand Up @@ -102,6 +102,10 @@ def files_to_gem_class_defs(files)
next
end
next if gem[:gem] == 'ruby'
# We're currently ignoring bundler, because we can't easily pin
# everyone to the same version of bundler in tests and in CI.
# There is an RBI for bundler in sorbet-typed.
next if gem[:gem] == 'bundler'

gem_class_defs[gem] ||= {}
defined.each do |item|
Expand Down
4 changes: 2 additions & 2 deletions gems/sorbet/lib/hidden-definition-finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def write_constants
puts "Printing your code's symbol table into #{SOURCE_CONSTANTS}"
io = IO.popen(
[
'srb',
File.realpath("#{__dir__}/../bin/srb"),
'tc',
'--print=symbol-table-json',
'--stdout-hup-hack',
Expand All @@ -153,7 +153,7 @@ def write_constants
Dir.chdir(TMP_PATH) do
io = IO.popen(
[
'srb',
File.realpath("#{__dir__}/../bin/srb"),
'tc',
'--print=symbol-table-json',
# Method redefined with mismatched argument is ok since sometime
Expand Down
2 changes: 1 addition & 1 deletion gems/sorbet/lib/suggest-typed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def self.main

def self.suggest_typed
IO.popen(
['srb', 'tc', '--suggest-typed', '--error-white-list=7022', '--typed=strict', '--silence-dev-message', '-a'],
[File.realpath("#{__dir__}/../bin/srb"), 'tc', '--suggest-typed', '--error-white-list=7022', '--typed=strict', '--silence-dev-message', '-a'],
err: [:child, :out],
) do |io|
out = io.read
Expand Down
2 changes: 1 addition & 1 deletion gems/sorbet/lib/todo-rbi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.main

IO.popen(
[
'srb',
File.realpath("#{__dir__}/../bin/srb"),
'tc',
'--print=missing-constants',
'--stdout-hup-hack',
Expand Down
96 changes: 96 additions & 0 deletions gems/sorbet/test/snapshot/driver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env bash

set -euo pipefail
jez marked this conversation as resolved.
Show resolved Hide resolved

cd "$(dirname "${BASH_SOURCE[0]}")/../.." || exit 1

# shellcheck disable=SC1091
source "test/snapshot/logging.sh"

# ----- Option parsing -----

usage() {
echo
echo "Usage:"
echo " $0 [options]"
echo
echo "Options:"
echo " --verbose Be more verbose (more than just test summary)."
echo " --update If a test fails, overwrite the expected with the actual"
}

VERBOSE=
UPDATE=
while [[ $# -gt 0 ]]; do
case $1 in
--verbose)
VERBOSE="--verbose"
shift
;;
--update)
UPDATE="--update"
shift
;;
-*)
echo
error "Unrecognized option: '$1'"
usage
exit 1
;;
*)
echo
error "Unrecognized positional arg: '$1'"
usage
exit 1
;;
esac
done

# ----- Discover and run all the tests -----

info "Running suite: $0 $VERBOSE $UPDATE"

if [ -z "$VERBOSE" ]; then
info "(re-run with --verbose for more information)"
else
info "(--verbose mode)"
fi

passing_tests=()
failing_tests=()

for test_dir in test/snapshot/{partial,total}/*; do
if test/snapshot/test_one.sh "$test_dir" $VERBOSE $UPDATE; then
passing_tests+=("test/snapshot/test_one.sh $test_dir $VERBOSE $UPDATE")
else
failing_tests+=("test/snapshot/test_one.sh $test_dir $VERBOSE $UPDATE")
fi
done


if [ "${#passing_tests[@]}" -ne 0 ]; then
echo
echo "───── Passing tests ─────"
for passing_test in "${passing_tests[@]}"; do
success "$passing_test"
done
fi

if [ "${#failing_tests[@]}" -ne 0 ]; then
echo
echo "───── Failing tests ─────"

for failing_test in "${failing_tests[@]}"; do
error "$failing_test"
done

if [ -z "$UPDATE" ]; then
echo
info "If you have reason to believe these differences are ok to keep, run"
info " $0 $* --update"
info "or append --update to any of the commands above to update a single test."
echo
fi

exit 1
fi
37 changes: 37 additions & 0 deletions gems/sorbet/test/snapshot/logging.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

red=$'\x1b[0;31m'
green=$'\x1b[0;32m'
yellow=$'\x1b[0;33m'
cyan=$'\x1b[0;36m'
cnone=$'\x1b[0m'

# Check if FD 1 is a tty
if [ -t 1 ]; then
jez marked this conversation as resolved.
Show resolved Hide resolved
USE_COLOR=1
else
USE_COLOR=0
fi

# Detects whether we can add colors or not
in_color() {
local color="$1"
shift

if [ "$USE_COLOR" = "1" ]; then
echo "$color$*$cnone"
else
echo "$*"
fi
}

success() { echo "$(in_color "$green" "[ OK ]") $*"; }
error() { echo "$(in_color "$red" "[ERR!]") $*"; }
info() { echo "$(in_color "$cyan" "[ .. ]") $*"; }
# Color entire warning to get users' attention (because we won't stop).
warn() { in_color "$yellow" "[ .. ] $*"; }

fatal() {
error "$@"
exit 1
}
Empty file.