msi_spec.rb 13.6 KB
Newer Older
Seth Vargo's avatar
Seth Vargo committed
1 2 3 4 5 6 7 8
require 'spec_helper'

module Omnibus
  describe Packager::MSI do
    let(:project) do
      Project.new.tap do |project|
        project.name('project')
        project.homepage('https://example.com')
9
        project.install_dir(install_dir)
Seth Vargo's avatar
Seth Vargo committed
10 11
        project.build_version('1.2.3')
        project.build_iteration('2')
12
        project.maintainer('Chef Software <maintainers@chef.io>')
Seth Vargo's avatar
Seth Vargo committed
13 14 15 16 17
      end
    end

    subject { described_class.new(project) }

18 19 20
    let(:project_root) { File.join(tmp_path, 'project/root') }
    let(:package_dir)  { File.join(tmp_path, 'package/dir') }
    let(:staging_dir)  { File.join(tmp_path, 'staging/dir') }
21
    let(:install_dir)  { 'C:/project' }
Seth Vargo's avatar
Seth Vargo committed
22 23 24 25 26 27 28 29 30 31 32 33 34

    before do
      Config.project_root(project_root)
      Config.package_dir(package_dir)

      allow(subject).to receive(:staging_dir).and_return(staging_dir)
      create_directory(staging_dir)
    end

    describe 'DSL' do
      it 'exposes :parameters' do
        expect(subject).to have_exposed_method(:parameters)
      end
Jay Mundrawala's avatar
Jay Mundrawala committed
35

36 37
      it 'exposes :signing_identity' do
        expect(subject).to have_exposed_method(:signing_identity)
Jay Mundrawala's avatar
Jay Mundrawala committed
38
      end
Seth Vargo's avatar
Seth Vargo committed
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
    end

    describe '#id' do
      it 'is :pkg' do
        expect(subject.id).to eq(:msi)
      end
    end

    describe '#upgrade_code' do
      it 'is a DSL method' do
        expect(subject).to have_exposed_method(:upgrade_code)
      end

      it 'is required' do
        expect {
          subject.upgrade_code
        }.to raise_error(MissingRequiredAttribute)
      end

      it 'requires the value to be a String' do
        expect {
          subject.parameters(Object.new)
        }.to raise_error(InvalidValue)
      end

      it 'returns the given value' do
        code = 'ABCD-1234'
        subject.upgrade_code(code)
        expect(subject.upgrade_code).to be(code)
      end
    end

    describe '#parameters' do
      it 'is a DSL method' do
        expect(subject).to have_exposed_method(:parameters)
      end

      it 'is defaults to an empty hash' do
        expect(subject.parameters).to be_a(Hash)
      end

      it 'requires the value to be a Hash' do
        expect {
          subject.parameters(Object.new)
        }.to raise_error(InvalidValue)
      end

      it 'returns the given value' do
        params = { 'Key' => 'value' }
        subject.parameters(params)
        expect(subject.parameters).to be(params)
      end
    end

    describe '#package_name' do
94 95 96 97
      before do
        allow(Config).to receive(:windows_arch).and_return(:foo_arch)
      end

Seth Vargo's avatar
Seth Vargo committed
98
      it 'includes the name, version, and build iteration' do
99
        expect(subject.package_name).to eq('project-1.2.3-2-foo_arch.msi')
Seth Vargo's avatar
Seth Vargo committed
100
      end
Thom May's avatar
Thom May committed
101 102 103

      it 'returns the bundle name when building a bundle' do
        subject.bundle_msi(true)
104
        expect(subject.package_name).to eq('project-1.2.3-2-foo_arch.exe')
Thom May's avatar
Thom May committed
105
      end
Seth Vargo's avatar
Seth Vargo committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    end

    describe '#resources_dir' do
      it 'is nested inside the staging_dir' do
        expect(subject.resources_dir).to eq("#{staging_dir}/Resources")
      end
    end

    describe '#write_localization_file' do
      it 'generates the file' do
        subject.write_localization_file
        expect("#{staging_dir}/localization-en-us.wxl").to be_a_file
      end

      it 'has the correct content' do
        subject.write_localization_file
        contents = File.read("#{staging_dir}/localization-en-us.wxl")

        expect(contents).to include('<String Id="ProductName">Project</String>')
125
        expect(contents).to include('<String Id="ManufacturerName">"Chef Software &lt;maintainers@chef.io&gt;"</String>')
Seth Vargo's avatar
Seth Vargo committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        expect(contents).to include('<String Id="FeatureMainName">Project</String>')
      end
    end

    describe '#write_parameters_file' do
      before do
        subject.upgrade_code('ABCD-1234')
      end

      it 'generates the file' do
        subject.write_parameters_file
        expect("#{staging_dir}/parameters.wxi").to be_a_file
      end

      it 'has the correct content' do
        subject.write_parameters_file
        contents = File.read("#{staging_dir}/parameters.wxi")

        expect(contents).to include('<?define VersionNumber="1.2.3.2" ?>')
        expect(contents).to include('<?define DisplayVersionNumber="1.2.3" ?>')
        expect(contents).to include('<?define UpgradeCode="ABCD-1234" ?>')
      end
    end

    describe '#write_source_file' do
      it 'generates the file' do
        subject.write_source_file
        expect("#{staging_dir}/source.wxs").to be_a_file
      end

      it 'has the correct content' do
157
        project.install_dir('C:/foo/bar/blip')
Seth Vargo's avatar
Seth Vargo committed
158 159 160 161
        subject.write_source_file
        contents = File.read("#{staging_dir}/source.wxs")

        expect(contents).to include('<?include "parameters.wxi" ?>')
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        expect(contents).to include <<-EOH.gsub(/^ {6}/, '')
          <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="WINDOWSVOLUME">
              <Directory Id="FOOLOCATION" Name="foo">
                <Directory Id="FOOBARLOCATION" Name="bar">
                  <Directory Id="PROJECTLOCATION" Name="blip">
                  </Directory>
                </Directory>
              </Directory>
            </Directory>
          </Directory>
        EOH
      end

      it 'has the correct wix_install_dir when the path is short' do
        subject.write_source_file
        contents = File.read("#{staging_dir}/source.wxs")

        expect(contents).to include('<?include "parameters.wxi" ?>')
        expect(contents).to include('<Property Id="WIXUI_INSTALLDIR" Value="WINDOWSVOLUME" />')
Seth Vargo's avatar
Seth Vargo committed
182 183 184
      end
    end

Thom May's avatar
Thom May committed
185 186 187 188
    describe '#write_bundle_file' do
      before do
        subject.bundle_msi(true)
        subject.upgrade_code('ABCD-1234')
189
        allow(Config).to receive(:windows_arch).and_return(:x86)
Thom May's avatar
Thom May committed
190 191 192 193 194 195 196 197
      end

      it 'generates the file' do
        subject.write_bundle_file
        expect("#{staging_dir}/bundle.wxs").to be_a_file
      end

      it 'has the correct content' do
198
        outpath = "#{tmp_path}/package/dir/project-1.2.3-2-x86.msi"
Thom May's avatar
Thom May committed
199 200 201 202 203 204 205
        outpath = outpath.gsub(File::SEPARATOR, File::ALT_SEPARATOR) if windows?
        subject.write_bundle_file
        contents = File.read("#{staging_dir}/bundle.wxs")
        expect(contents).to include("<MsiPackage SourceFile=\"#{outpath}\" EnableFeatureSelection=\"no\" />")
      end
    end

Seth Vargo's avatar
Seth Vargo committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    describe '#msi_version' do
      context 'when the project build_version semver' do
        it 'returns the right value' do
          expect(subject.msi_version).to eq('1.2.3.2')
        end
      end

      context 'when the project build_version is git' do
        before { project.build_version('1.2.3-alpha.1+20140501194641.git.94.561b564') }

        it 'returns the right value' do
          expect(subject.msi_version).to eq('1.2.3.2')
        end
      end
    end

    describe '#msi_display_version' do
      context 'when the project build_version is "safe"' do
        it 'returns the right value' do
          expect(subject.msi_display_version).to eq('1.2.3')
        end
      end

      context 'when the project build_version is a git tag' do
        before { project.build_version('1.2.3-alpha.1+20140501194641.git.94.561b564') }

        it 'returns the right value' do
          expect(subject.msi_display_version).to eq('1.2.3')
        end
      end
    end
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

    describe '#wix_candle_extensions' do
      it 'defaults to an empty Array' do
        expect(subject.wix_candle_extensions).to be_an(Array)
        expect(subject.wix_candle_extensions).to be_empty
      end
    end

    describe '#wix_light_extensions' do
      it 'defaults to an empty Array' do
        expect(subject.wix_light_extensions).to be_an(Array)
        expect(subject.wix_light_extensions).to be_empty
      end
    end

    describe '#wix_candle_extension' do
      it 'is a DSL method' do
        expect(subject).to have_exposed_method(:wix_candle_extension)
      end

      it 'requires the value to be an String' do
        expect {
          subject.wix_candle_extension(Object.new)
        }.to raise_error(InvalidValue)
      end

      it 'returns the given value' do
        extensions = ['a']
        subject.wix_candle_extension(extensions[0])
        expect(subject.wix_candle_extensions).to match_array(extensions)
      end
    end

    describe '#wix_light_extension' do
      it 'is a DSL method' do
        expect(subject).to have_exposed_method(:wix_light_extension)
      end

      it 'requires the value to be an String' do
        expect {
          subject.wix_light_extension(Object.new)
        }.to raise_error(InvalidValue)
      end

      it 'returns the given value' do
        extensions = ['a']
        subject.wix_light_extension(extensions[0])
        expect(subject.wix_light_extensions).to match_array(extensions)
      end
    end

    describe '#wix_extension_switches' do
      it 'returns an empty string for an empty array' do
        expect(subject.wix_extension_switches([])).to eq('')
      end

      it 'returns the correct value for one extension' do
        expect(subject.wix_extension_switches(['a'])).to eq("-ext 'a'")
      end

      it 'returns the correct value for many extensions' do
        expect(subject.wix_extension_switches(['a', 'b'])).to eq("-ext 'a' -ext 'b'")
      end
    end
Jay Mundrawala's avatar
Jay Mundrawala committed
301

302
    describe '#bundle_msi' do
Thom May's avatar
Thom May committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
      it 'is a DSL method' do
        expect(subject).to have_exposed_method(:bundle_msi)
      end

      it 'requires the value to be a TrueClass or a FalseClass' do
        expect {
          subject.bundle_msi(Object.new)
        }.to raise_error(InvalidValue)
      end

      it 'returns the given value' do
        subject.bundle_msi(true)
        expect(subject.bundle_msi).to be_truthy
      end
    end

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    describe '#gem_path' do
      let(:install_dir) { File.join(tmp_path, 'install_dir') }

      before do
        create_directory(install_dir)
      end

      after do
        remove_directory(install_dir)
      end

      it 'is a DSL method' do
        expect(subject).to have_exposed_method(:gem_path)
      end

      it 'requires the value to be a String' do
        expect {
          subject.gem_path(Object.new)
        }.to raise_error(InvalidValue)
      end

      it 'globs for gems under the install directory' do
        expected_gem_path = 'something/gems/athing-1.0.0'
        create_directory(File.join(install_dir, expected_gem_path))
        expect(subject.gem_path('athing-*')).to eq(expected_gem_path)
      end

      it 'returns the gem directory when no argument is given' do
        expected_gem_path = 'foo/bar123/gems'
        create_directory(File.join(install_dir, expected_gem_path))
        expect(subject.gem_path).to eq(expected_gem_path)
      end
    end

Jay Mundrawala's avatar
Jay Mundrawala committed
353 354 355
    context 'when signing parameters are provided' do
      let(:msi) { 'somemsi.msi' }

356 357 358 359 360 361 362 363 364 365 366 367 368 369
      context 'when invalid parameters' do
        it 'should raise an InvalidValue error when the certificate name is not a String' do
          expect{subject.signing_identity(Object.new)}.to raise_error(InvalidValue)
        end

        it 'should raise an InvalidValue error when params is not a Hash' do
          expect{subject.signing_identity("foo", Object.new)}.to raise_error(InvalidValue)
        end

        it 'should raise an InvalidValue error when params contains an invalid key' do
          expect{subject.signing_identity("foo", bar: 'baz')}.to raise_error(InvalidValue)
        end
      end

Jay Mundrawala's avatar
Jay Mundrawala committed
370 371 372 373 374 375
      context 'when valid parameters' do
        before do
          allow(subject).to receive(:shellout!)
        end

        it 'should sign the file and then add the timestamp' do
376
          subject.signing_identity('foo')
Jay Mundrawala's avatar
Jay Mundrawala committed
377 378 379 380
          expect(subject).to receive(:add_timestamp)
          subject.sign_package(msi)
        end

381
        describe '#timestamp_servers' do
Jay Mundrawala's avatar
Jay Mundrawala committed
382
          it "defaults to using ['http://timestamp.digicert.com','http://timestamp.verisign.com/scripts/timestamp.dll']" do
383
            subject.signing_identity('foo')
Jay Mundrawala's avatar
Jay Mundrawala committed
384 385 386 387 388 389
            expect(subject).to receive(:try_timestamp).with(msi, 'http://timestamp.digicert.com').and_return(false)
            expect(subject).to receive(:try_timestamp).with(msi, 'http://timestamp.verisign.com/scripts/timestamp.dll').and_return(true)
            subject.sign_package(msi)
          end

          it 'uses the timestamp server if provided through the #timestamp_server dsl' do
390
            subject.signing_identity('foo', timestamp_servers: 'http://fooserver')
Jay Mundrawala's avatar
Jay Mundrawala committed
391 392 393 394 395
            expect(subject).to receive(:try_timestamp).with(msi, 'http://fooserver').and_return(true)
            subject.sign_package(msi)
          end

          it 'tries all timestamp server if provided through the #timestamp_server dsl' do
396
            subject.signing_identity('foo', timestamp_servers: ['http://fooserver', 'http://barserver'])
Jay Mundrawala's avatar
Jay Mundrawala committed
397 398 399 400 401
            expect(subject).to receive(:try_timestamp).with(msi, 'http://fooserver').and_return(false)
            expect(subject).to receive(:try_timestamp).with(msi, 'http://barserver').and_return(true)
            subject.sign_package(msi)
          end

402 403 404 405
          it 'tries all timestamp server if provided through the #timestamp_servers dsl and stops at the first available' do
            subject.signing_identity('foo', timestamp_servers: ['http://fooserver', 'http://barserver'])
            expect(subject).to receive(:try_timestamp).with(msi, 'http://fooserver').and_return(true)
            expect(subject).not_to receive(:try_timestamp).with(msi, 'http://barserver')
Jay Mundrawala's avatar
Jay Mundrawala committed
406 407 408 409
            subject.sign_package(msi)
          end

          it 'raises an exception if there are no available timestamp servers' do
410
            subject.signing_identity('foo', timestamp_servers: 'http://fooserver')
Jay Mundrawala's avatar
Jay Mundrawala committed
411
            expect(subject).to receive(:try_timestamp).with(msi, 'http://fooserver').and_return(false)
412
            expect {subject.sign_package(msi)}.to raise_error(FailedToTimestampMSI)
Jay Mundrawala's avatar
Jay Mundrawala committed
413 414 415 416
          end
        end
      end
    end
Seth Vargo's avatar
Seth Vargo committed
417 418
  end
end