Skip to content

Commit

Permalink
test: moved Tebako Cli tests from shunit to rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed Aug 4, 2024
1 parent 1fb9527 commit 2a7c228
Show file tree
Hide file tree
Showing 23 changed files with 408 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.name: lint

name: lint
name: lint-and-rspec

on:
push:
Expand Down Expand Up @@ -54,3 +54,23 @@ jobs:

- name: Run rubocop
run: bundle exec rubocop

rspec:
runs-on: ubuntu-latest
env:
CODECOV_TOKEN: 795f17c3-2dc3-4253-9627-e2d0a2e59223
steps:
- uses: actions/checkout@v4

- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.1'
bundler-cache: true

- name: Run rspec
run: bundle exec rspec

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ env.CODECOV_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@ tebako_test.log
.t/
tests-actions/
.environment.version

# rspec failure tracking
.rspec_status
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ Naming/FileName:

Style/BlockComments:
Enabled: false

Gemspec/DevelopmentDependencies:
EnforcedStyle: gemspec
4 changes: 0 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,3 @@ source "https://rubygems.org"

# Specify your gem's dependencies in tebako.gemspec
gemspec
gem "hoe"
gem "minitest"
gem "rubocop", "~> 1.52"
gem "rubocop-rubycw"
6 changes: 4 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright (c) 2023 [Ribose Inc](https://www.ribose.com).
# Copyright (c) 2023-2024 [Ribose Inc](https://www.ribose.com).
# All rights reserved.
# This file is a part of tebako
#
Expand All @@ -27,10 +27,12 @@

require "bundler/gem_tasks"
require "rubocop/rake_task"
require "rspec/core/rake_task"

RuboCop::RakeTask.new
RSpec::Core::RakeTask.new(:spec)

task default: %i[rubocop]
task default: %i[rubocop spec]

desc "Generate version.txt"
task "generate_version_txt" do
Expand Down
16 changes: 8 additions & 8 deletions lib/tebako/cli_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,25 @@ def cfg_options
# So we have to use \"xxx\"
@cfg_options ||=
"-DCMAKE_BUILD_TYPE=Release -DRUBY_VER:STRING=\"#{ruby_ver}\" -DRUBY_HASH:STRING=\"#{ruby_hash}\" " \
"-DDEPS:STRING=\"#{deps}\" -G \"#{m_files}\" -B \"#{output}\" -S \"#{source}\""
"-DDEPS:STRING=\"#{deps}\" -G \"#{m_files}\" -B \"#{output_folder}\" -S \"#{source}\""
end

def clean_cache
puts "Cleaning tebako packaging environment"
# Using File.join(deps, "") to ensure that the slashes are appropriate
FileUtils.rm_rf([File.join(deps, ""), File.join(output, "")], secure: true)
FileUtils.rm_rf([File.join(deps, ""), File.join(output_folder, "")], secure: true)

Check warning on line 64 in lib/tebako/cli_helpers.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/cli_helpers.rb#L64

Added line #L64 was not covered by tests
end

def clean_output
puts "Cleaning CMake cache and Ruby build files"
# Using File.join(output, "") to ensure that the slashes are appropriate

nmr = "src/_ruby_*"
nms = "stash_*"
FileUtils.rm_rf(Dir.glob(File.join(deps, nmr)), secure: true)
FileUtils.rm_rf(Dir.glob(File.join(deps, nms)), secure: true)

FileUtils.rm_rf(File.join(output, ""), secure: true)
# Using File.join(output_folder, "") to ensure that the slashes are appropriate
FileUtils.rm_rf(File.join(output_folder, ""), secure: true)

Check warning on line 76 in lib/tebako/cli_helpers.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/cli_helpers.rb#L76

Added line #L76 was not covered by tests
end

def deps
Expand All @@ -82,15 +82,15 @@ def deps

def do_press
cfg_cmd = "cmake -DSETUP_MODE:BOOLEAN=OFF #{cfg_options} #{press_options}"
build_cmd = "cmake --build #{output} --target tebako --parallel #{Etc.nprocessors}"
build_cmd = "cmake --build #{output_folder} --target tebako --parallel #{Etc.nprocessors}"
merged_env = ENV.to_h.merge(b_env)
Tebako.packaging_error(103) unless system(merged_env, cfg_cmd)
Tebako.packaging_error(104) unless system(merged_env, build_cmd)

Check warning on line 88 in lib/tebako/cli_helpers.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/cli_helpers.rb#L84-L88

Added lines #L84 - L88 were not covered by tests
end

def do_setup
cfg_cmd = "cmake -DSETUP_MODE:BOOLEAN=ON #{cfg_options}"
build_cmd = "cmake --build \"#{output}\" --target setup --parallel #{Etc.nprocessors}"
build_cmd = "cmake --build \"#{output_folder}\" --target setup --parallel #{Etc.nprocessors}"
merged_env = ENV.to_h.merge(b_env)
Tebako.packaging_error(101) unless system(merged_env, cfg_cmd)
Tebako.packaging_error(102) unless system(merged_env, build_cmd)

Check warning on line 96 in lib/tebako/cli_helpers.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/cli_helpers.rb#L92-L96

Added lines #L92 - L96 were not covered by tests
Expand Down Expand Up @@ -156,8 +156,8 @@ def options_from_tebafile(tebafile)
{}
end

def output
@output ||= File.join(prefix, "o")
def output_folder
@output_folder ||= File.join(prefix, "o")

Check warning on line 160 in lib/tebako/cli_helpers.rb

View check run for this annotation

Codecov / codecov/patch

lib/tebako/cli_helpers.rb#L160

Added line #L160 was not covered by tests
end

def package
Expand Down
124 changes: 124 additions & 0 deletions spec/cli_helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# frozen_string_literal: true

# Copyright (c) 2024 [Ribose Inc](https://www.ribose.com).
# All rights reserved.
# This file is a part of tebako
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

require "yaml"
require_relative "../lib/tebako/cli_helpers"

RSpec.describe Tebako::CliHelpers do # rubocop:disable Metrics/BlockLength
include Tebako::CliHelpers

let(:options) { {} }

before do
allow(self).to receive(:options).and_return(options)
end

describe "#l_level" do
context "when log-level option is not set" do
it 'returns "error"' do
expect(l_level).to eq("error")
end
end

context "when log-level option is set" do
let(:options) { { "log-level" => "info" } }

it "returns the value of the log-level option" do
expect(l_level).to eq("info")
end
end
end

describe "#m_files" do # rubocop:disable Metrics/BlockLength
context "when on a Linux platform" do
before do
stub_const("RUBY_PLATFORM", "linux")
end

it 'returns "Unix Makefiles"' do
expect(m_files).to eq("Unix Makefiles")
end
end

context "when on a macOS platform" do
before do
stub_const("RUBY_PLATFORM", "darwin")
end

it 'returns "Unix Makefiles"' do
expect(m_files).to eq("Unix Makefiles")
end
end

context "when on a Windows platform" do
before do
stub_const("RUBY_PLATFORM", "msys")
end

it 'returns "MinGW Makefiles"' do
expect(m_files).to eq("MinGW Makefiles")
end
end

context "when on an unsupported platform" do
before do
stub_const("RUBY_PLATFORM", "unsupported")
end

it "raises a Tebako::Error" do
expect { m_files }.to raise_error(Tebako::Error, "unsupported is not supported yet, exiting")
end
end
end

describe "#options_from_tebafile" do
let(:tebafile) { "spec/fixtures/tebafile.yml" }

context "when the tebafile contains valid YAML" do
it "loads options from the tebafile" do
allow(YAML).to receive(:load_file).and_return({ "options" => { "key" => "value" } })
expect(options_from_tebafile(tebafile)).to eq({ "key" => "value" })
end
end

context "when the tebafile contains invalid YAML" do
it "returns an empty hash and prints a warning" do
allow(YAML).to receive(:load_file).and_raise(Psych::SyntaxError.new("file", 1, 1, 1, "message", "context"))
expect { options_from_tebafile(tebafile) }.to output(/Warning: The tebafile/).to_stdout
expect(options_from_tebafile(tebafile)).to eq({})
end
end

context "when an unexpected error occurs" do
it "returns an empty hash and prints a warning" do
allow(YAML).to receive(:load_file).and_raise(StandardError.new("Unexpected error"))
expect { options_from_tebafile(tebafile) }.to output(/An unexpected error occurred/).to_stdout
expect(options_from_tebafile(tebafile)).to eq({})
end
end
end
end
54 changes: 54 additions & 0 deletions spec/cli_rubies_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

# Copyright (c) 2024 [Ribose Inc](https://www.ribose.com).
# All rights reserved.
# This file is a part of tebako
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

require "tebako/cli_rubies"

RSpec.describe Tebako::CliRubies do
include Tebako::CliRubies

describe "#version_check" do
context "when the Ruby version is supported" do
it "does not raise an error" do
expect { version_check("3.1.6") }.not_to raise_error
end
end

context "when the Ruby version is not supported" do
it "raises a Tebako::Error" do
expect do
version_check("2.6.0")
end.to raise_error(Tebako::Error, "Ruby version 2.6.0 is not supported yet, exiting")
end
end
end

describe "DEFAULT_RUBY_VERSION" do
it "is set to 3.1.6" do
expect(Tebako::CliRubies::DEFAULT_RUBY_VERSION).to eq("3.1.6")
end
end
end
73 changes: 73 additions & 0 deletions spec/error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

# Copyright (c) 2024 [Ribose Inc](https://www.ribose.com).
# All rights reserved.
# This file is a part of tebako
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

RSpec.describe Tebako do # rubocop:disable Metrics/BlockLength
describe "PACKAGING_ERRORS" do # rubocop:disable Metrics/BlockLength
it "is defined" do
expect(defined?(Tebako::PACKAGING_ERRORS)).to be_truthy
end

describe ".packaging_error" do
it "raises the correct error for known error codes" do
expect { Tebako.packaging_error(101) }.to raise_error(Tebako::Error, "'tebako setup' configure step failed")
expect do
Tebako.packaging_error(106)
end.to raise_error(Tebako::Error, "Entry point does not exist or is not accessible")
end

it "raises a generic error message for unknown error codes" do
expect { Tebako.packaging_error(999) }.to raise_error(Tebako::Error, "Unknown packaging error")
end
end

describe "Error" do
it "is defined" do
expect(defined?(Tebako::Error)).to be_truthy
end

it "inherits from StandardError" do
expect(Tebako::Error).to be < StandardError
end

it "can be instantiated" do
expect { Tebako::Error.new }.not_to raise_error
end

it "initializes with the correct message and error code" do
error = Tebako::Error.new("Custom error message", 123)
expect(error.message).to eq("Custom error message")
expect(error.error_code).to eq(123)
end

it "has an accessible and modifiable error_code attribute" do
error = Tebako::Error.new
error.error_code = 456
expect(error.error_code).to eq(456)
end
end
end
end
Loading

0 comments on commit 2a7c228

Please sign in to comment.