Add Ruby test CI (#1242)

* test: add test all Ruby code

* test: add ruby test ci

* Update ruby.yml

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
khoaxuantu 2024-04-09 02:20:04 +07:00 committed by GitHub
parent 62572bf576
commit 41dd677338
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 60 additions and 0 deletions

37
.github/workflows/ruby.yml vendored Normal file
View file

@ -0,0 +1,37 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by separate terms of service, privacy policy, and support documentation.
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake。
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
name: Ruby
on:
push:
branches: [ "main" ]
paths: ["codes/ruby/**/*.rb"]
pull_request:
branches: [ "main" ]
paths: ["codes/ruby/**/*.rb"]
workflow_dispatch:
permissions:
contents: read
jobs:
test:
name: Ruby ${{ matrix.ruby-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
ruby-version: ['3.3']
steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
- name: Run tests
run: ruby codes/ruby/test_all.rb

23
codes/ruby/test_all.rb Normal file
View file

@ -0,0 +1,23 @@
require 'open3'
start_time = Time.now
ruby_code_dir = File.dirname(__FILE__)
files = Dir.glob("#{ruby_code_dir}/chapter_*/*.rb")
errors = []
files.each do |file|
stdout, stderr, status = Open3.capture3("ruby #{file}")
errors << stderr unless status.success?
end
puts "\x1b[34mTested #{files.count} files\x1b[m"
unless errors.empty?
puts "\x1b[33mFound exception in #{errors.length} files\x1b[m"
raise errors.join("\n\n")
else
puts "\x1b[32mPASS\x1b[m"
end
puts "Testing finishes after #{((Time.now - start_time) * 1000).round} ms"