Script specs.

This commit is contained in:
Aaron Klaassen 2022-01-04 15:26:59 -06:00
parent 3bc9dbc6cc
commit 1dc131a0f0
5 changed files with 105 additions and 52 deletions

View file

@ -4,27 +4,6 @@ require "json"
require "octokit"
require_relative "lib/github"
require_relative "lib/commenter"
require_relative "lib/action"
if !ENV["GITHUB_TOKEN"]
puts "Missing GITHUB_TOKEN"
exit(1)
end
if ARGV[0].nil? || ARGV[0].empty?
puts "Missing message argument."
exit(1)
end
commenter = Commenter.new(github: GitHub.new(env: ENV),
message: ARGV[0],
check_for_duplicates: ARGV[1],
duplicate_pattern: ARGV[2],
delete_previous_pattern: ARGV[3])
if commenter.block_duplicates? && commenter.existing_duplicates?
puts "The PR already contains this message"
exit(0)
end
commenter.delete_matching_comments!
commenter.comment!
exit(run)

26
lib/action.rb Normal file
View file

@ -0,0 +1,26 @@
def run
if !ENV["GITHUB_TOKEN"]
puts "Missing GITHUB_TOKEN"
return 1
end
if ARGV[0].nil? || ARGV[0].empty?
puts "Missing message argument."
return 1
end
commenter = Commenter.new(github: GitHub.new(env: ENV),
message: ARGV[0],
check_for_duplicates: ARGV[1],
duplicate_pattern: ARGV[2],
delete_previous_pattern: ARGV[3])
if commenter.block_duplicates? && commenter.existing_duplicates?
puts "The PR already contains this message"
return 0
end
commenter.delete_matching_comments!
commenter.comment!
return 0
end

76
spec/action_spec.rb Normal file
View file

@ -0,0 +1,76 @@
require "spec_helper"
RSpec.describe "action.rb" do
let(:base_env) do
{
"GITHUB_TOKEN" => "secret",
"GITHUB_EVENT_PATH" => File.join(__dir__, "event.json")
}
end
before :each do
stub_const("ARGV", ["hello"])
end
describe "required arguments" do
it "fails without GITHUB_TOKEN" do
expect { run }.to output(/Missing GITHUB_TOKEN/).to_stdout
expect(run).to eq 1
end
it "fails without message" do
stub_const("ENV", base_env)
stub_const("ARGV", [])
expect { run }.to output(/Missing message argument/).to_stdout
expect(run).to eq 1
end
end
describe "blocking duplicate comments" do
before :each do
stub_const("ENV", base_env)
end
context "we are blocking duplicates" do
before :each do
allow_any_instance_of(Commenter).to receive(:block_duplicates?).and_return(true)
end
it "exits when there is already a matching comment" do
allow_any_instance_of(Commenter).to receive(:existing_duplicates?).and_return(true)
expect { run }.to output(/The PR already contains this message/).to_stdout
expect(run).to eq 0
end
it "comments when no match" do
allow_any_instance_of(Commenter).to receive(:existing_duplicates?).and_return(false)
allow_any_instance_of(Commenter).to receive(:comment!)
expect { run }.to_not output.to_stdout
expect(run).to eq 0
end
end
context "not blocking duplicates" do
before :each do
allow_any_instance_of(Commenter).to receive(:block_duplicates?).and_return(false)
end
it "comments even with match" do
allow_any_instance_of(Commenter).to receive(:existing_duplicates?).and_return(true)
allow_any_instance_of(Commenter).to receive(:comment!)
expect { run }.to_not output.to_stdout
expect(run).to eq 0
end
it "comments when no match" do
allow_any_instance_of(Commenter).to receive(:existing_duplicates?).and_return(false)
allow_any_instance_of(Commenter).to receive(:comment!)
expect { run }.to_not output.to_stdout
expect(run).to eq 0
end
end
end
end

View file

@ -1,28 +0,0 @@
require "spec_helper"
RSpec.describe "entrypoint.sh" do
def run_script(env = {}, args = [])
path = File.join(__dir__, "../entrypoint.sh")
Open3.capture3(env, path, *args)
end
let(:base_env) do
{
"GITHUB_TOKEN" => "secret",
"GITHUB_EVENT_PATH" => File.join(__dir__, "event.json")
}
end
it "fails without GITHUB_TOKEN" do
out, err, status = run_script
expect(out).to eq "Missing GITHUB_TOKEN\n"
expect(status.success?).to eq false
end
it "fails without message" do
out, err, status = run_script(base_env)
expect(out).to eq "Missing message argument.\n"
expect(status.success?).to eq false
end
end

View file

@ -1,5 +1,5 @@
require "json"
require "octokit"
require "open3"
require_relative "../lib/github"
require_relative "../lib/commenter"
require_relative "../lib/action"