Merge pull request #10727 from chrisroberts/f-empty-id-file

Remove whitespace from id file on load
This commit is contained in:
Chris Roberts 2019-03-20 15:47:14 -07:00 committed by GitHub
commit 0462da57c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 1 deletions

View File

@ -399,7 +399,10 @@ module Vagrant
# Read the id file from the data directory if it exists as the
# ID for the pre-existing physical representation of this machine.
id_file = @data_dir.join("id")
@id = id_file.read.chomp if id_file.file?
id_content = id_file.read.strip if id_file.file?
if !id_content.to_s.empty?
@id = id_content
end
end
if @id != old_id && @provider

View File

@ -912,4 +912,62 @@ describe Vagrant::Machine do
expect(subject.ui).to_not equal(ui)
end
end
describe "#reload" do
context "when ID is unset and id file does not exist" do
it "should remain nil" do
expect(subject.id).to be_nil
instance.reload
expect(subject.id).to be_nil
end
end
context "when id file is set" do
let(:id_content) { "test-machine-id" }
before do
id_file = subject.data_dir.join("id")
File.write(id_file.to_s, id_content)
end
it "should update the machine id" do
expect(subject.id).to be_nil
instance.reload
expect(subject.id).to eq(id_content)
end
it "should notify of the id change when provider is set" do
expect(provider).to receive(:machine_id_changed)
instance.reload
end
context "when id file content includes whitespace" do
let(:id_content) { " test-machine-id\n" }
it "should remove all whitespace" do
instance.reload
expect(instance.id).to eq("test-machine-id")
end
end
context "when id file content is all whitespace" do
let(:id_content) { "\0\0\0\0\0\0" }
it "should not update the id" do
expect(instance.id).to be_nil
instance.reload
expect(instance.id).to be_nil
end
end
context "when id is already set to value in id file" do
it "should not notify of id change" do
instance.instance_variable_set(:@id, id_content)
expect(provider).not_to receive(:machine_id_changed)
instance.reload
expect(instance.id).to eq(id_content)
end
end
end
end
end