Add optional arguments as a hash

This commit is contained in:
sophia 2020-06-18 11:24:27 -05:00
parent decd489a6d
commit 7322c74eef
2 changed files with 23 additions and 9 deletions

View File

@ -25,12 +25,14 @@ module VagrantPlugins
#
# @param [Vagrant::Environment] env
# @param [String] source_directory Contents of ISO
# @param [String, nil] file_destination Location to store ISO
# @param [Map] extra arguments to pass to the iso building command
# :file_destination (string) location to store ISO
# :volume_id (String) to set the volume name
# @return [Pathname] ISO location
# @note If file_destination exists, source_directory will be checked
# for recent modifications and a new ISO will be generated if requried.
def self.create_iso(env, source_directory, file_destination=nil, extra_opts={})
def self.create_iso(env, source_directory, **extra_opts)
file_destination = extra_opts[:file_destination]
source_directory = Pathname.new(source_directory)
if file_destination.nil?
@@logger.info("No file destination specified, creating temp location")
@ -42,7 +44,7 @@ module VagrantPlugins
# If the file destination path is a folder, target the output to a randomly named
# file in that dir
if file_destination.extname != ".iso"
file_destination = file_destination.join("#{rand(36**6).to_s(36)}_vagrant.iso")
file_destination = file_destination.join("#{SecureRandom.hex(3)}_vagrant.iso")
end
end
# Ensure destination directory is available
@ -56,7 +58,7 @@ module VagrantPlugins
iso_command << "-iso"
iso_command << "-joliet"
iso_command << "-ov"
iso_command.concat(Vagrant::Util::MapCommandOptions.map_to_command_options(extra_opts, cmd_flag="-"))
iso_command.concat(["-default-volume-name", extra_opts[:volume_id]]) if extra_opts[:volume_id]
iso_command << "-o"
iso_command << file_destination.to_s
iso_command << source_directory.to_s

View File

@ -33,16 +33,16 @@ describe VagrantPlugins::HostDarwin::Cap::FsISO do
"hdiutil", "makehybrid", "-hfs", "-iso", "-joliet", "-ov", "-o", /.iso/, /\/foo\/src/
).and_return(double(exit_code: 0))
output = subject.create_iso(env, "/foo/src", "/woo/out.iso")
output = subject.create_iso(env, "/foo/src", file_destination: "/woo/out.iso")
expect(output.to_s).to eq("/woo/out.iso")
end
it "builds an iso with args" do
it "builds an iso with volume_id" do
expect(Vagrant::Util::Subprocess).to receive(:execute).with(
"hdiutil", "makehybrid", "-hfs", "-iso", "-joliet", "-ov", "-default-volume-name", "cidata", "-o", /.iso/, /\/foo\/src/
).and_return(double(exit_code: 0))
output = subject.create_iso(env, "/foo/src", "/woo/out.iso", extra_opts={"default-volume-name" => "cidata"})
output = subject.create_iso(env, "/foo/src", file_destination: "/woo/out.iso", volume_id: "cidata")
expect(output.to_s).to eq("/woo/out.iso")
end
@ -51,13 +51,25 @@ describe VagrantPlugins::HostDarwin::Cap::FsISO do
"hdiutil", "makehybrid", "-hfs", "-iso", "-joliet", "-ov", "-o", /.iso/, /\/foo\/src/
).and_return(double(exit_code: 0))
output = subject.create_iso(env, "/foo/src", "/woo/out_dir")
output = subject.create_iso(env, "/foo/src", file_destination: "/woo/out_dir")
expect(output.to_s).to match(/\/woo\/out_dir\/[\w]{6}_vagrant.iso/)
end
it "builds an iso when no file destination is given" do
allow(Tempfile).to receive(:new).and_return(file_destination)
allow(file_destination).to receive(:path).and_return(file_destination)
allow(file_destination).to receive(:delete)
expect(Vagrant::Util::Subprocess).to receive(:execute).with(
"hdiutil", "makehybrid", "-hfs", "-iso", "-joliet", "-ov", "-o", /.iso/, /\/foo\/src/
).and_return(double(exit_code: 0))
output = subject.create_iso(env, "/foo/src")
expect(output.to_s).to eq(file_destination)
end
it "raises an error if iso build failed" do
allow(Vagrant::Util::Subprocess).to receive(:execute).with(any_args).and_return(double(stdout: "nope", stderr: "nope", exit_code: 1))
expect{ subject.create_iso(env, "/foo/src", "/woo/out.iso") }.to raise_error(Vagrant::Errors::ISOBuildFailed)
expect{ subject.create_iso(env, "/foo/src") }.to raise_error(Vagrant::Errors::ISOBuildFailed)
end
end
end