Skip to content

Commit 16b1a61

Browse files
committed
updating rspec tests
1 parent 04f2acb commit 16b1a61

File tree

8 files changed

+282
-24
lines changed

8 files changed

+282
-24
lines changed

.github/workflows/rspec.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: RSpec
2+
on: [push, pull_request]
3+
4+
jobs:
5+
rspec:
6+
strategy:
7+
fail-fast: false
8+
matrix:
9+
os: [ubuntu-latest]
10+
ruby: [2.7]
11+
runs-on: ${{ matrix.os }}
12+
steps:
13+
- uses: actions/checkout@v2
14+
- uses: ruby/setup-ruby@v1
15+
with:
16+
ruby-version: ${{ matrix.ruby }}
17+
bundler-cache: true
18+
- name: RSpec run
19+
run: |
20+
bash -c "
21+
bundle exec rspec
22+
[[ $? -ne 2 ]]
23+
"
24+
rspec-mri:
25+
needs: rspec
26+
strategy:
27+
fail-fast: false
28+
matrix:
29+
os: [ ubuntu-latest ]
30+
ruby: [2.5, 2.6, '3.0', head]
31+
runs-on: ${{ matrix.os }}
32+
steps:
33+
- uses: actions/checkout@v2
34+
- uses: ruby/setup-ruby@v1
35+
with:
36+
ruby-version: ${{ matrix.ruby }}
37+
bundler-cache: true
38+
- run: bundle exec rspec
39+
rspec-truffleruby:
40+
needs: rspec
41+
strategy:
42+
fail-fast: false
43+
matrix:
44+
os: [ ubuntu-latest ]
45+
ruby: [truffleruby]
46+
runs-on: ${{ matrix.os }}
47+
steps:
48+
- uses: actions/checkout@v2
49+
- uses: ruby/setup-ruby@v1
50+
with:
51+
ruby-version: ${{ matrix.ruby }}
52+
bundler-cache: true
53+
- run: bundle exec rspec

lib/telemetry/logger/builder.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
require 'logger'
2+
13
module Telemetry
24
module Logger
35
module Builder
6+
def opts
7+
@opts ||= {}
8+
end
9+
410
def format(include_pid: false, **)
511
log.formatter = proc do |severity, datetime, _progname, msg|
612
string = "[#{datetime}]"
@@ -11,11 +17,17 @@ def format(include_pid: false, **)
1117
end
1218

1319
def log
14-
@log ||= output
20+
@log ||= output(**opts)
1521
end
1622

1723
def output(**options)
24+
return @log unless @log.nil?
25+
1826
@log = ::Logger.new(options[:log_file] || $stdout)
27+
self.log_level = options[:level] if options.key? :level
28+
self.format
29+
30+
@log
1931
end
2032

2133
def level

lib/telemetry/logger/defaults.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'socket'
2+
13
module Telemetry
24
module Logger
35
module Defaults
@@ -6,15 +8,19 @@ def socket_hostname
68
end
79

810
def env_prefix
9-
@opts[:env_prefix] || 'telemetry'
11+
opts[:env_prefix] || 'telemetry'
1012
end
1113

1214
def application
13-
opts.key?(:application) ? @opts[:application] : 'telemetry'
15+
opts[:application] || 'telemetry'
1416
end
1517

1618
def app_version
17-
opts.key?(:app_version) ? @opts[:app_version] : Telemetry::Telemetry::VERSION
19+
opts[:app_version] || Telemetry::Logger::VERSION
20+
end
21+
22+
def opts
23+
@opts ||= {}
1824
end
1925
end
2026
end

lib/telemetry/logger/methods.rb

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
module Telemetry
22
module Logger
33
module Methods
4-
def opts
5-
@opts ||= {}
6-
end
7-
84
def colorize
9-
opts[:color] || opts[:logfile].is_a?(String) || true
10-
end
11-
12-
def trace(raw_message = nil, size: @trace_size, log_caller: true) # rubocop:disable Metrics/AbcSize
13-
return unless @trace_enabled
14-
15-
raw_message = yield if raw_message.nil? && block_given?
16-
message = Rainbow('Tracing: ').cyan
17-
message.concat Rainbow("#{raw_message} ").cyan
18-
if log_caller && size.nil?
19-
message.concat Rainbow(caller_locations).cyan.underline
20-
elsif log_caller
21-
message.concat Rainbow(caller_locations[0..size]).cyan.underline
22-
end
23-
log.unknown(message)
5+
@colorize ||= if opts[:logfile].is_a?(String)
6+
false
7+
elsif opts.key? :color
8+
opts[:color]
9+
else
10+
true
11+
end
2412
end
2513

2614
def debug(message = nil)

spec/builder_spec.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
require 'spec_helper'
2+
require 'telemetry/logger/version'
3+
require 'telemetry/logger/builder'
4+
5+
RSpec.describe Telemetry::Logger::Builder do
6+
it { should be_a Module }
7+
before :all do
8+
@class = Class.new do
9+
def initialize(**opts)
10+
@opts = opts
11+
end
12+
include Telemetry::Logger::Builder
13+
end
14+
end
15+
16+
it 'should respond to log' do
17+
expect(@class.new(level: 'debug').level).to eq 0
18+
expect(@class.new(level: 'info').level).to eq 1
19+
expect(@class.new(level: 'warn').level).to eq 2
20+
expect(@class.new(level: 'error').level).to eq 3
21+
expect(@class.new(level: 'fatal').level).to eq 4
22+
end
23+
24+
it 'should respond to level' do
25+
expect(@class.new.log_level = 'warn').to eq 'warn'
26+
expect(@class.new.level).to eq 0
27+
new_class = @class.new(level: 'warn')
28+
new_class.log_level = 'warn'
29+
expect(new_class.level).to eq 2
30+
end
31+
32+
it 'can modify the log level' do
33+
modify_level = @class.new
34+
expect(modify_level.level).to eq 0
35+
expect { modify_level.log_level = 'debug' }.not_to raise_exception
36+
expect(modify_level.level).to eq 0
37+
expect { modify_level.log_level = 'info' }.not_to raise_exception
38+
expect(modify_level.level).to eq 1
39+
expect { modify_level.log_level = 'warn' }.not_to raise_exception
40+
expect(modify_level.level).to eq 2
41+
expect { modify_level.log_level = 'error' }.not_to raise_exception
42+
expect(modify_level.level).to eq 3
43+
expect { modify_level.log_level = 'fatal' }.not_to raise_exception
44+
expect(modify_level.level).to eq 4
45+
46+
expect(modify_level.log_level = nil).to eq nil
47+
expect(modify_level.level).to eq 42
48+
49+
expect(modify_level.log_level = 'foobar').to eq 'foobar'
50+
expect(modify_level.level).to eq 0
51+
52+
expect(modify_level.log_level = 111).to eq 111
53+
expect(modify_level.level).to eq 111
54+
end
55+
56+
it 'can call the formatter' do
57+
expect(@class.new.format).to be_a Proc
58+
expect(@class.new.format(include_pid: true)).to be_a Proc
59+
end
60+
end

spec/defaults_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'spec_helper'
2+
require 'telemetry/logger/version'
3+
require 'telemetry/logger/defaults'
4+
5+
RSpec.describe Telemetry::Logger::Defaults do
6+
it { should be_a Module }
7+
before :all do
8+
@class = Class.new do
9+
def initialize(**opts)
10+
@opts = opts
11+
end
12+
include Telemetry::Logger::Defaults
13+
end
14+
end
15+
16+
it 'should have an env_prefix' do
17+
expect(@class.new.env_prefix).to eq 'telemetry'
18+
expect(@class.new(env_prefix: 'foobar').env_prefix).to eq 'foobar'
19+
end
20+
21+
it 'should have a socket_hostname' do
22+
expect(@class.new.socket_hostname).to be_a String
23+
end
24+
25+
it 'should have an application' do
26+
expect(@class.new.application).to eq 'telemetry'
27+
expect(@class.new(application: 'foobar').application).to eq 'foobar'
28+
end
29+
30+
it 'should have an app_version' do
31+
expect(@class.new.app_version).to eq Telemetry::Logger::VERSION
32+
expect(@class.new(app_version: 'foobar').app_version).to eq 'foobar'
33+
end
34+
end

spec/logger_spec.rb

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
11
require 'spec_helper'
2+
require 'telemetry/logger'
23

34
RSpec.describe Telemetry::Logger do
45
it { should be_a Module }
6+
7+
# before :all do
8+
# Telemetry::Logger.setup(level: 'debug')
9+
# end
510
it 'should have a version number' do
6-
expect(described_class::VERSION).to be_a String
11+
expect(Telemetry::Logger::VERSION).to be_a String
12+
end
13+
14+
it 'should have a setup command' do
15+
expect(Telemetry::Logger).to respond_to :setup
16+
end
17+
18+
it 'should be able to log debug' do
19+
expect { Telemetry::Logger.debug('testing') }.to output(/DEBUG/).to_stdout_from_any_process
20+
end
21+
22+
it 'should be able to log info' do
23+
expect { Telemetry::Logger.info('testing') }.to output(/INFO/).to_stdout_from_any_process
24+
end
25+
26+
it 'should be able to log warn' do
27+
expect { Telemetry::Logger.warn('testing') }.to output(/WARN/).to_stdout_from_any_process
28+
end
29+
30+
it 'should be able to log error' do
31+
expect { Telemetry::Logger.error('testing') }.to output(/ERROR/).to_stdout_from_any_process
32+
end
33+
34+
it 'should be able to log fatal' do
35+
expect { Telemetry::Logger.fatal('testing') }.to output(/FATAL/).to_stdout_from_any_process
36+
end
37+
38+
it 'should be able to log unknown' do
39+
expect { Telemetry::Logger.unknown('testing') }.to output(/ANY/).to_stdout_from_any_process
740
end
841
end

spec/methods_spec.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
require 'spec_helper'
2+
require 'telemetry/logger/version'
3+
require 'telemetry/logger/methods'
4+
5+
RSpec.describe Telemetry::Logger::Methods do
6+
it { should be_a Module }
7+
before :all do
8+
@class = Class.new do
9+
include Telemetry::Logger::Methods
10+
include Telemetry::Logger::Builder
11+
12+
def initialize(**opts)
13+
@opts = opts
14+
end
15+
16+
def opts
17+
@opts ||= {}
18+
end
19+
end
20+
end
21+
22+
it 'can colorize' do
23+
expect(@class.new(color: false).colorize).to eq false
24+
expect(@class.new(color: true).colorize).to eq true
25+
expect(@class.new.colorize).to eq true
26+
expect(@class.new(color: true, logfile: './test.log').colorize).to eq false
27+
end
28+
29+
it 'can debug' do
30+
expect { @class.new.debug('testing') }.to output(/DEBUG/).to_stdout_from_any_process
31+
expect { @class.new(level: 'debug').debug('testing') }.to output(/DEBUG/).to_stdout_from_any_process
32+
expect { @class.new(level: 'info').debug('testing') }.not_to output(/DEBUG/).to_stdout_from_any_process
33+
expect { @class.new(level: 'warn').debug('testing') }.not_to output(/DEBUG/).to_stdout_from_any_process
34+
expect { @class.new(level: 'error').debug('testing') }.not_to output(/DEBUG/).to_stdout_from_any_process
35+
expect { @class.new(level: 'fatal').debug('testing') }.not_to output(/DEBUG/).to_stdout_from_any_process
36+
end
37+
38+
it 'can info' do
39+
expect { @class.new.info('testing') }.to output(/INFO/).to_stdout_from_any_process
40+
expect { @class.new(level: 'info').info('testing') }.to output(/INFO/).to_stdout_from_any_process
41+
expect { @class.new(level: 'info').debug('testing') }.not_to output(/DEBUG/).to_stdout_from_any_process
42+
end
43+
44+
it 'can warn' do
45+
expect { @class.new.warn('testing') }.to output(/WARN/).to_stdout_from_any_process
46+
expect { @class.new(level: 'warn').warn('testing') }.to output(/WARN/).to_stdout_from_any_process
47+
end
48+
49+
it 'can error' do
50+
expect { @class.new.error('testing') }.to output(/ERROR/).to_stdout_from_any_process
51+
expect { @class.new(level: 'error').error('testing') }.to output(/ERROR/).to_stdout_from_any_process
52+
end
53+
54+
it 'can fatal' do
55+
expect { @class.new.fatal('testing') }.to output(/FATAL/).to_stdout_from_any_process
56+
expect { @class.new(level: 'fatal').fatal('testing') }.to output(/FATAL/).to_stdout_from_any_process
57+
end
58+
59+
it 'can unknown' do
60+
expect { @class.new.unknown('testing') }.to output(/ANY/).to_stdout_from_any_process
61+
end
62+
63+
it 'can return thread info' do
64+
expect(@class.new.thread).to be_a String
65+
expect(@class.new.thread(kvl: false)).to be_a String
66+
expect(@class.new.thread(kvl: true)).to be_a String
67+
end
68+
69+
it 'has an opts hash' do
70+
expect(@class.new.opts).to eq({})
71+
end
72+
end

0 commit comments

Comments
 (0)