Skip to content

Commit

Permalink
Replace OS gem
Browse files Browse the repository at this point in the history
  • Loading branch information
kapoorlakshya authored Jul 20, 2024
1 parent 1750086 commit e960020
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lib/screen-recorder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'logger'
require 'os'
require 'streamio-ffmpeg'

# @since 1.0.0.beta11
Expand Down Expand Up @@ -80,4 +79,5 @@ def self.logger
require 'screen-recorder/screenshot'
require 'screen-recorder/desktop'
require 'screen-recorder/window'
require 'screen-recorder/childprocess'
require 'screen-recorder/childprocess'
require 'screen-recorder/os'
92 changes: 92 additions & 0 deletions lib/screen-recorder/os.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
module ScreenRecorder
# @api private
module OS
module_function

def home
@home ||= Dir.home
end

def engine
@engine ||= RUBY_ENGINE.to_sym
end

def os
host_os = RbConfig::CONFIG['host_os']
@os ||= case host_os
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
:windows
when /darwin|mac os/
:macosx
when /linux/
:linux
when /solaris|bsd/
:unix
else
raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
end
end

def jruby?
engine == :jruby
end

def truffleruby?
engine == :truffleruby
end

def ruby_version
RUBY_VERSION
end

def windows?
os == :windows
end

def mac?
os == :macosx
end

def linux?
os == :linux
end

def unix?
os == :unix
end

def wsl?
return false unless linux?

File.read('/proc/version').downcase.include?('microsoft')
rescue Errno::EACCES
# the file cannot be accessed on Linux on DeX
false
end

def cygwin?
RUBY_PLATFORM.include?('cygwin')
end

def null_device
File::NULL
end

def cygwin_path(path, only_cygwin: false, **opts)
return path if only_cygwin && !cygwin?

flags = []
opts.each { |k, v| flags << "--#{k}" if v }

`cygpath #{flags.join ' '} "#{path}"`.strip
end

def unix_path(path)
path.tr(File::ALT_SEPARATOR, File::SEPARATOR)
end

def windows_path(path)
path.tr(File::SEPARATOR, File::ALT_SEPARATOR)
end
end # OS
end
1 change: 0 additions & 1 deletion screen-recorder.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@ Gem::Specification.new do |spec|

spec.require_paths = ['lib']

spec.add_dependency 'os', '~> 1.0'
spec.add_dependency 'streamio-ffmpeg', '~> 3.0'
end
2 changes: 1 addition & 1 deletion spec/screen-recorder/desktop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
end

# @todo Figure out how to test this on Travis since default is 1 and Travis uses 0.
unless OS.mac?
unless ScreenRecorder::OS.mac?
it 'defaults to OS specific input if none is given' do
expect(described_class.new(output: test_output).options.input).to eq(test_input)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/screen-recorder/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
expect { described_class.new(bad_test_options) }.to raise_exception(ArgumentError)
end

it 'defaults input pixel format to uyvy422 on macOS', if: OS.mac? do
it 'defaults input pixel format to uyvy422 on macOS', if: ScreenRecorder::OS.mac? do
expect(options.advanced[:input][:pix_fmt]).to eql('uyvy422')
end

it 'does not default input pixel format to yuv420p on Windows and Linux', if: !OS.mac? do
it 'does not default input pixel format to yuv420p on Windows and Linux', if: !ScreenRecorder::OS.mac? do
expect(options.advanced[:input][:pix_fmt]).to be_nil
end

Expand Down
2 changes: 1 addition & 1 deletion spec/screen-recorder/screenshot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
end
end

context 'when recording a window', if: OS.windows? do
context 'when recording a window', if: ScreenRecorder::OS.windows? do
let!(:browser) { Watir::Browser.new :chrome, options: { args: ['--disable-gpu'] } }
let(:recorder) do
page_title = ScreenRecorder::Window.fetch_title('chrome').first
Expand Down
8 changes: 4 additions & 4 deletions spec/screen-recorder/titles_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
RSpec.describe ScreenRecorder::Titles do
describe '.fetch' do
context 'when the user is using Linux or MacOS', if: OS.linux? || OS.mac? do
context 'when the user is using Linux or MacOS', if: ScreenRecorder::OS.linux? || ScreenRecorder::OS.mac? do
it 'raises error when OS is not Microsoft Windows' do
# @todo Raise StandardError instead.
expect { described_class.fetch('firefox') }.to raise_error(NotImplementedError)
end
end
end

context 'when the given application is Firefox', if: OS.windows? do
context 'when the given application is Firefox', if: ScreenRecorder::OS.windows? do
let(:browser_process) { :firefox }
let(:url) { 'https://google.com' }
let(:expected_title) { /Google [-—] Mozilla Firefox/ }
Expand Down Expand Up @@ -40,13 +40,13 @@
end
end

context 'when a firefox window is not open', if: OS.windows? do
context 'when a firefox window is not open', if: ScreenRecorder::OS.windows? do
it 'raises an exception' do
expect { described_class.fetch('firefox') }.to raise_exception(ScreenRecorder::Errors::ApplicationNotFound)
end
end

context 'when application is Chrome with extensions as individual processes', if: OS.windows? do
context 'when application is Chrome with extensions as individual processes', if: ScreenRecorder::OS.windows? do
let(:browser_process) { :chrome }
let(:url) { 'https://google.com' }
let(:expected_titles) { ['Google - Google Chrome'] }
Expand Down
2 changes: 1 addition & 1 deletion spec/screen-recorder/window_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Only gdigrab supports window capture
RSpec.describe ScreenRecorder::Window, if: OS.windows? do
RSpec.describe ScreenRecorder::Window, if: ScreenRecorder::OS.windows? do
let(:test_website) { 'https://google.com' }

it 'raises an error when a title is not given' do
Expand Down
25 changes: 13 additions & 12 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'bundler/setup'
require 'simplecov'
require_relative '../lib/screen-recorder/os'

SimpleCov.start do
add_filter %r{/spec/}
Expand Down Expand Up @@ -27,14 +28,14 @@
delete_file '*.log'

# Start xvfb if not running on Linux
if OS.linux?
if ScreenRecorder::OS.linux?
running = `pgrep Xvfb`.strip
`Xvfb -ac ":0" -screen 0 1024x768x24 > /dev/null 2>&1 &` unless running
end
end

config.after(:suite) do
if OS.linux? || OS.mac?
if ScreenRecorder::OS.linux? || ScreenRecorder::OS.mac?
# check for any unexpectedly abandoned ffmpeg processes
sleep(2) # wait any straglers to terminate
running = `pgrep -f ffmpeg`.strip
Expand All @@ -55,14 +56,14 @@
end

#
# Returns input value for tests to use based on current OS.
# Returns input value for tests to use based on current ScreenRecorder::OS.
#
def test_input
if OS.linux?
if ScreenRecorder::OS.linux?
ENV.fetch('DISPLAY', ':0').strip
elsif OS.mac?
elsif ScreenRecorder::OS.mac?
ENV['CI'] ? '0' : '1'
elsif OS.windows?
elsif ScreenRecorder::OS.windows?
'desktop'
else
raise 'Your OS is not supported. Feel free to create an Issue on GitHub.'
Expand All @@ -75,7 +76,7 @@ def test_input
def test_output
file = "recording-#{Time.now.to_i}.mkv"
path = File.join(Dir.pwd, file)
return path.tr('/', '\\') if OS.windows?
return path.tr('/', '\\') if ScreenRecorder::OS.windows?

path
end
Expand All @@ -86,7 +87,7 @@ def test_output
def test_log_file
file = 'ffmpeg.log'
path = File.join(Dir.pwd, file)
return path.tr('/', '\\') if OS.windows?
return path.tr('/', '\\') if ScreenRecorder::OS.windows?

path
end
Expand Down Expand Up @@ -119,14 +120,14 @@ def test_options
end

#
# Returns capture device based on the current OS.
# Returns capture device based on the current ScreenRecorder::OS.
#
def test_capture_device
return 'gdigrab' if OS.windows?
return 'gdigrab' if ScreenRecorder::OS.windows?

return 'x11grab' if OS.linux?
return 'x11grab' if ScreenRecorder::OS.linux?

'avfoundation' if OS.mac?
'avfoundation' if ScreenRecorder::OS.mac?
end

#
Expand Down

0 comments on commit e960020

Please sign in to comment.