Write to random env variables
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module EnvironmentWriter
|
||||
def set_output(name, value)
|
||||
modify_env("GITHUB_OUTPUT", name, value)
|
||||
end
|
||||
|
||||
def set_environment(name, value)
|
||||
modify_env("GITHUB_ENV", name, value)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def modify_env(file, name, value)
|
||||
return if value.nil?
|
||||
|
||||
File.open(ENV[file], "a") do |f|
|
||||
f.puts "#{name}=#{value}"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,11 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module OutputWriter
|
||||
def set_output(name, value)
|
||||
return if value.nil?
|
||||
|
||||
File.open(ENV["GITHUB_OUTPUT"], "a") do |f|
|
||||
f.puts "#{name}=#{value}"
|
||||
end
|
||||
end
|
||||
end
|
||||
+14
-4
@@ -1,9 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "../concerns/output_writer"
|
||||
require_relative "../concerns/environment_writer"
|
||||
|
||||
class Arguments
|
||||
include OutputWriter
|
||||
include EnvironmentWriter
|
||||
|
||||
def initialize(provider, command, issue_content)
|
||||
@args = argument_class(provider, command, issue_content)
|
||||
@@ -24,12 +24,22 @@ class Arguments
|
||||
|
||||
arguments.concat(["--custom-transformers", *@custom_transformers]) if @custom_transformers.length.positive?
|
||||
|
||||
# rng = ENV["CI"] ? Random.new(0) : Random.new
|
||||
rng = Random.new(0)
|
||||
|
||||
set_output(
|
||||
"args",
|
||||
arguments.map do |a|
|
||||
next a unless a.include?(" ")
|
||||
value = a.include?(" ") ? a.inspect : a
|
||||
|
||||
unless value.start_with?("--")
|
||||
name = "variable_#{rng.rand(0..1000)}"
|
||||
set_environment(name, value)
|
||||
|
||||
value = "$#{name}"
|
||||
end
|
||||
|
||||
a.inspect
|
||||
value
|
||||
end.join(" ")
|
||||
)
|
||||
end
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "active_support/core_ext/string"
|
||||
require_relative "../concerns/output_writer"
|
||||
require_relative "../concerns/environment_writer"
|
||||
|
||||
class Command
|
||||
include OutputWriter
|
||||
include EnvironmentWriter
|
||||
|
||||
VALID_COMMANDS = %w[audit migrate dry-run].freeze
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ require_rel "./jenkins/**/*.rb"
|
||||
require_rel "./travis_ci/**/*.rb"
|
||||
|
||||
class Provider
|
||||
include OutputWriter
|
||||
include EnvironmentWriter
|
||||
|
||||
PROVIDER_MAP = {
|
||||
"azure-devops" => ::AzureDevops,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe OutputWriter do
|
||||
RSpec.describe EnvironmentWriter do
|
||||
let(:test_class) do
|
||||
class TestClass
|
||||
include OutputWriter
|
||||
include EnvironmentWriter
|
||||
end
|
||||
|
||||
TestClass.new
|
||||
@@ -24,4 +24,20 @@ RSpec.describe OutputWriter do
|
||||
it { expect { subject }.not_to change { File.read(ENV["GITHUB_OUTPUT"], chomp: true).last } }
|
||||
end
|
||||
end
|
||||
|
||||
describe "#set_environment" do
|
||||
let(:name) { "env_var_name" }
|
||||
let(:value) { "env_var_value" }
|
||||
let(:output) { "#{name}=#{value}" }
|
||||
|
||||
subject { test_class.set_environment(name, value) }
|
||||
|
||||
it { expect { subject }.to change { File.readlines(ENV["GITHUB_ENV"], chomp: true).last }.to(/#{output}/) }
|
||||
|
||||
context "when value is nil" do
|
||||
let(:value) { nil }
|
||||
|
||||
it { expect { subject }.not_to change { File.read(ENV["GITHUB_ENV"], chomp: true).last } }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -54,7 +54,8 @@ RSpec.describe Arguments do
|
||||
let(:output) { ["--option", "value"] }
|
||||
|
||||
it "writes an output variable" do
|
||||
expect(arguments).to receive(:set_output).with("args", "--option value")
|
||||
expect(arguments).to receive(:set_output).with("args", "--option $variable_1")
|
||||
expect(arguments).to receive(:set_environment).with("variable_684", "value")
|
||||
subject
|
||||
end
|
||||
end
|
||||
|
||||
@@ -29,6 +29,7 @@ RSpec.configure do |config|
|
||||
Dir.mkdir("tmp") unless Dir.exist?("tmp")
|
||||
FileUtils.touch "tmp/test.txt"
|
||||
ENV["GITHUB_OUTPUT"] = "tmp/test.txt"
|
||||
ENV["GITHUB_ENV"] = "tmp/test.txt"
|
||||
end
|
||||
|
||||
config.after(:suite) do
|
||||
|
||||
Reference in New Issue
Block a user