File: command_test.rb

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (140 lines) | stat: -rw-r--r-- 5,509 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require_relative "../../../base"
require_relative "../../../../../plugins/commands/init/command"

tests_root = Pathname(__FILE__).join("../../../../../..")

describe VagrantPlugins::CommandInit::Command do
  include_context "unit"
  include_context "command plugin helpers"

  let(:iso_env) do
    isolated_environment
  end

  let(:env) do
    iso_env.create_vagrant_env
  end

  let(:vagrantfile_path) { File.join(env.cwd, "Vagrantfile") }

  before do
    allow(Vagrant.plugin("2").manager).to receive(:commands).and_return({})
  end

  after do
    iso_env.close
  end

  describe "#execute" do
    it "creates a Vagrantfile with no args" do
      described_class.new([], env).execute
      contents = File.read(vagrantfile_path)
      expect(contents).to match(/config.vm.box = "base"/)
    end

    it "creates a minimal Vagrantfile" do
      described_class.new(["-m"], env).execute
      contents = File.read(vagrantfile_path)
      expect(contents).to_not match(/provision/)
    end

    it "creates a custom Vagrantfile using a relative template path" do
      described_class.new(["--template", "test/unit/templates/commands/init/Vagrantfile"], env).execute
      contents = File.read(vagrantfile_path)
      expect(contents).to match(/config.vm.hostname = "vagrant.dev"/)
    end

    it "creates a custom Vagrantfile using an absolute template path" do
      described_class.new(["--template", tests_root.join("test/unit/templates/commands/init/Vagrantfile").to_s], env).execute
      contents = File.read(vagrantfile_path)
      expect(contents).to match(/config.vm.hostname = "vagrant.dev"/)
    end

    it "creates a custom Vagrantfile using a provided template with the extension included" do
      described_class.new(["--template", tests_root.join("test/unit/templates/commands/init/Vagrantfile.erb").to_s], env).execute
      contents = File.read(vagrantfile_path)
      expect(contents).to match(/config.vm.hostname = "vagrant.dev"/)
    end

    it "creates a custom Vagrant file using a template provided from the environment" do
      with_temp_env("VAGRANT_DEFAULT_TEMPLATE" => "test/unit/templates/commands/init/Vagrantfile") do
        described_class.new([], env).execute
      end
      contents = File.read(vagrantfile_path)
      expect(contents).to match(/config.vm.hostname = "vagrant.dev"/)
    end

    it "ignores the environmentally-set default template when a template is explicitly set" do
      with_temp_env("VAGRANT_DEFAULT_TEMPLATE" => "/this_file_does_not_exist") do
        described_class.new(["--template", "test/unit/templates/commands/init/Vagrantfile"], env).execute
      end
      contents = File.read(vagrantfile_path)
      expect(contents).to match(/config.vm.hostname = "vagrant.dev"/)
    end

    it "ignores the -m option when using a provided template" do
      described_class.new(["-m", "--template", tests_root.join("test/unit/templates/commands/init/Vagrantfile").to_s], env).execute
      contents = File.read(vagrantfile_path)
      expect(contents).to match(/config.vm.hostname = "vagrant.dev"/)
    end

    it "raises an appropriate exception when the template file can't be found" do
      expect {
      described_class.new(["--template", "./a/b/c/template"], env).execute
      }.to raise_error(Vagrant::Errors::VagrantfileTemplateNotFoundError)
    end

    it "does not overwrite an existing Vagrantfile" do
      # Create an existing Vagrantfile
      File.open(File.join(env.cwd, "Vagrantfile"), "w+") { |f| f.write("") }

      expect {
        described_class.new([], env).execute
      }.to raise_error(Vagrant::Errors::VagrantfileExistsError)
    end

    it "overwrites an existing Vagrantfile with force" do
      # Create an existing Vagrantfile
      File.open(File.join(env.cwd, "Vagrantfile"), "w+") { |f| f.write("") }

      expect {
        described_class.new(["-f"], env).execute
      }.to_not raise_error

      contents = File.read(vagrantfile_path)
      expect(contents).to match(/config.vm.box = "base"/)
    end

    it "creates a Vagrantfile with a box" do
      described_class.new(["hashicorp/precise64"], env).execute
      contents = File.read(vagrantfile_path)
      expect(contents).to match(/config.vm.box = "hashicorp\/precise64"/)
    end

    it "creates a Vagrantfile with a box and box_url" do
      described_class.new(["hashicorp/precise64", "http://example.com"], env).execute
      contents = File.read(vagrantfile_path)
      expect(contents).to match(/config.vm.box = "hashicorp\/precise64"/)
      expect(contents).to match(/config.vm.box_url = "http:\/\/example.com"/)
    end

    it "creates a Vagrantfile with a box and box version" do
      described_class.new(["--box-version", "1.2.3", "hashicorp/precise64"], env).execute
      contents = File.read(vagrantfile_path)
      expect(contents).to match(/config.vm.box = "hashicorp\/precise64"/)
      expect(contents).to match(/config.vm.box_version = "1.2.3"/)
    end

    it "creates a minimal Vagrantfile with a box and box version" do
      described_class.new(["--minimal", "--box-version", "1.2.3", "hashicorp/precise64"], env).execute
      contents = File.read(vagrantfile_path)
      expect(contents).to match(/config.vm.box = "hashicorp\/precise64"/)
      expect(contents).to match(/config.vm.box_version = "1.2.3"/)
    end

    it "creates a Vagrantfile at a custom path" do
      described_class.new(["--output", "vf.rb"], env).execute
      expect(File.exist?(File.join(env.cwd, "vf.rb"))).to be(true)
    end
  end
end