File: support_spec.rb

package info (click to toggle)
ruby-tty-color 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 180 kB
  • sloc: ruby: 450; sh: 4; makefile: 4
file content (161 lines) | stat: -rw-r--r-- 5,598 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# frozen_string_literal: true

RSpec.describe TTY::Color::Support, "#support?" do
  it "doesn't check color support for non tty terminal" do
    support = described_class.new({})
    allow(TTY::Color).to receive(:tty?).and_return(false)
    expect(support.support?).to eq(false)
  end

  it "disables color support when NO_COLOR is set" do
    support = described_class.new({"NO_COLOR" => "1"})
    expect(support.support?).to eq(false)
    expect(support).to be_disabled
  end

  it "fails to find out color support" do
    support = described_class.new({})
    allow(TTY::Color).to receive(:tty?).and_return(true)

    allow(support).to receive(:from_curses).and_return(TTY::Color::NoValue)
    allow(support).to receive(:from_tput).and_return(TTY::Color::NoValue)
    allow(support).to receive(:from_term).and_return(TTY::Color::NoValue)
    allow(support).to receive(:from_env).and_return(TTY::Color::NoValue)

    expect(support.support?).to eq(false)

    expect(support).to have_received(:from_term).ordered
    expect(support).to have_received(:from_tput).ordered
    expect(support).to have_received(:from_env).ordered
    expect(support).to have_received(:from_curses).ordered
  end

  it "detects color support" do
    support = described_class.new({"TERM" => "xterm"})
    allow(TTY::Color).to receive(:tty?).and_return(true)
    allow(support).to receive(:from_tput)

    expect(support.support?).to eq(true)
    expect(support).to_not have_received(:from_tput)
  end

  context "#from_curses" do
    it "fails to load curses for color support" do
      support = described_class.new({})
      allow(TTY::Color).to receive(:windows?).and_return(false)
      allow(support).to receive(:require).with("curses").and_raise(LoadError)
      allow(support).to receive(:warn)

      expect(support.from_curses).to eq(TTY::Color::NoValue)
      expect(support).to_not have_received(:warn)
    end

    it "fails to find Curses namespace" do
      support = described_class.new({})
      allow(TTY::Color).to receive(:windows?).and_return(false)
      allow(support).to receive(:require).with("curses")

      expect(support.from_curses).to eq(TTY::Color::NoValue)
    end

    it "sets verbose mode on" do
      support = described_class.new({}, verbose: true)
      allow(TTY::Color).to receive(:windows?).and_return(false)
      allow(support).to receive(:require).with("curses").and_raise(LoadError)
      allow(support).to receive(:warn)

      support.from_curses

      expect(support).to have_received(:warn).with(/no native curses support/)
    end

    it "loads curses for color support" do
      support = described_class.new({})
      allow(TTY::Color).to receive(:windows?).and_return(false)
      allow(support).to receive(:require).with("curses").and_return(true)
      stub_const("Curses", Object.new)
      curses = double(:curses)
      allow(curses).to receive(:init_screen)
      allow(curses).to receive(:has_colors?).and_return(true)
      allow(curses).to receive(:close_screen)

      expect(support.from_curses(curses)).to eql(true)
      expect(curses).to have_received(:has_colors?)
    end

    it "doesn't check on windows" do
      support = described_class.new({})
      allow(TTY::Color).to receive(:windows?).and_return(true)
      expect(support.from_curses).to eq(TTY::Color::NoValue)
    end
  end

  context "#from_term" do
    it "fails to find color for dumb terminal" do
      support = described_class.new({"TERM" => "dumb"})
      expect(support.from_term).to eq(false)
    end

    it "inspects term variable for color capabilities" do
      support = described_class.new({"TERM" => "xterm"})
      expect(support.from_term).to eq(true)
      support = described_class.new({"TERM" => "tmux-256color"})
      expect(support.from_term).to eq(true)
    end

    it "fails to find color capabilities from term variable " do
      support = described_class.new({"TERM" => "atari"})
      expect(support.from_term).to eq(TTY::Color::NoValue)
    end
  end

  context "#from_tput" do
    it "fails to find tput utilty" do
      support = described_class.new({})
      allow(TTY::Color).to receive(:command?).with("tput colors").and_return(nil)

      expect(support.from_tput).to eq(TTY::Color::NoValue)
    end

    it "runs tput and detects 8 colors" do
      allow(TTY::Color).to receive(:command?).with("tput colors").and_return(true)
      support = described_class.new({})
      allow(support).to receive(:`).and_return("8")

      expect(support.from_tput).to eq(true)
    end

    it "runs tput but finds no colors" do
      allow(TTY::Color).to receive(:command?).with("tput colors").and_return(true)
      support = described_class.new({})
      allow(support).to receive(:`).and_return("2")

      expect(support.from_tput).to eq(false)
    end

    it "raises error when running tput" do
      allow(TTY::Color).to receive(:command?).with("tput colors").and_return(true)
      support = described_class.new({})
      allow(support).to receive(:`).and_raise(Errno::ENOENT)

      expect(support.from_tput).to eq(TTY::Color::NoValue)
    end
  end

  context "#from_env" do
    it "finds color support in colorterm variable" do
      support = described_class.new({"COLORTERM" => true})
      expect(support.from_env).to eq(true)
    end

    it "finds ansicon support" do
      support = described_class.new({"ANSICON" => true})
      expect(support.from_env).to eq(true)
    end

    it "doesn't find any keys in environment" do
      support = described_class.new({})
      expect(support.from_env).to eq(TTY::Color::NoValue)
    end
  end
end