aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/cask/system_command_result_spec.rb
blob: 4a077de7b3c6b25d2dfec8b63ce80c880a1a862b (plain)
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
require "hbc/system_command"

describe Hbc::SystemCommand::Result, :cask do
  describe "::_parse_plist" do
    subject { described_class._parse_plist(command, input) }
    let(:command) { Hbc::SystemCommand.new("/usr/bin/true", {}) }
    let(:plist) {
      <<-EOS.undent
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
        <plist version="1.0">
        <dict>
          <key>system-entities</key>
          <array>
            <dict>
              <key>content-hint</key>
              <string>Apple_partition_map</string>
              <key>dev-entry</key>
              <string>/dev/disk3s1</string>
              <key>potentially-mountable</key>
              <false/>
              <key>unmapped-content-hint</key>
              <string>Apple_partition_map</string>
            </dict>
            <dict>
              <key>content-hint</key>
              <string>Apple_partition_scheme</string>
              <key>dev-entry</key>
              <string>/dev/disk3</string>
              <key>potentially-mountable</key>
              <false/>
              <key>unmapped-content-hint</key>
              <string>Apple_partition_scheme</string>
            </dict>
            <dict>
              <key>content-hint</key>
              <string>Apple_HFS</string>
              <key>dev-entry</key>
              <string>/dev/disk3s2</string>
              <key>mount-point</key>
              <string>/private/tmp/dmg.BhfS2g</string>
              <key>potentially-mountable</key>
              <true/>
              <key>unmapped-content-hint</key>
              <string>Apple_HFS</string>
              <key>volume-kind</key>
              <string>hfs</string>
            </dict>
          </array>
        </dict>
        </plist>
      EOS
    }

    context "when output contains garbage" do
      let(:input) {
        <<-EOS.undent
          Hello there! I am in no way XML am I?!?!

            That's a little silly... you were expexting XML here!

          What is a parser to do?

          Hopefully <not> explode!

          #{plist}
        EOS
      }

      it "ignores garbage before xml" do
        expect(subject.keys).to eq(["system-entities"])
        expect(subject["system-entities"].length).to eq(3)
      end
    end

    context "given a hdiutil output as input" do
      let(:input) { plist }

      it "successfully parses it" do
        expect(subject.keys).to eq(["system-entities"])
        expect(subject["system-entities"].length).to eq(3)
        expect(subject["system-entities"].map { |e| e["dev-entry"] })
          .to eq(["/dev/disk3s1", "/dev/disk3", "/dev/disk3s2"])
      end
    end

    context "given an empty input" do
      let(:input) { "" }

      it "raises an error" do
        expect { subject }.to raise_error(Hbc::CaskError, /Empty plist input/)
      end
    end
  end
end