Files
ubicloud/spec/routes/api/cli/spec_helper.rb
Jeremy Evans 60e129bd84 Fix the specs to avoid defining methods on Object
Defining methods inside RSpec.configure blocks still defines them on
Object.  Use config.include with a module to define these only in the
specs and not globally.

For matcher methods, define them in RSpec::Matchers::DSL::Matcher.
Change rubocop config to allow this inside of the RSpec.configure
blocks (technically, any constant inside any block).

Add an Object.method_added hook that raises an exception if defining
a method on object, other than Nokogiri, which is defined by the
nokogiri gem. This will result in the specs raising an error without
executing anything if you attempt to define a method on Object.
2025-05-03 03:33:24 +09:00

40 lines
1.4 KiB
Ruby

# frozen_string_literal: true
require_relative "../spec_helper"
RSpec.configure do |config|
config.include(Module.new do
def cli(argv, status: 200, env: {"HTTP_X_UBI_VERSION" => "1.0.0"}, confirm_prompt: nil, command_execute: nil)
post("/cli", {"argv" => argv}.to_json, env)
expect(last_response.status).to eq(status), "status is #{last_response.status} not #{status}, body for failing status: #{last_response.body}"
if status == 400
expect(last_response.body).to start_with("! ")
end
expect(last_response["content-type"]).to eq("text/plain")
expect(last_response["ubi-command-execute"]).to eq(command_execute) if command_execute
expect(last_response["ubi-confirm"]).to eq(confirm_prompt) if confirm_prompt
if !last_response["ubi-command-execute"] && !last_response["ubi-confirm"]
expect(last_response.body).to end_with("\n")
end
last_response.body
end
def cli_exec(argv, env: {})
body = cli(argv, env:)
[last_response.headers["ubi-command-execute"], *body.split("\0")]
end
end)
config.define_derived_metadata(file_path: %r{\A\./spec/routes/api/cli/}) do |metadata|
metadata[:clover_cli] = true
end
config.before do |example|
next unless example.metadata[:clover_cli]
header "Accept", "text/plain"
@account = create_account
@use_pat = true
@project = project_with_default_policy(@account, name: "project-1")
end
end