nyan-cat-formatter 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +0 -1
- data/lib/nyan_cat_format/music.rb +35 -32
- data/nyan-cat-formatter.gemspec +1 -1
- data/spec/nyan_cat_music_formatter_spec.rb +40 -5
- metadata +2 -2
    
        data/.travis.yml
    CHANGED
    
    
| @@ -1,8 +1,6 @@ | |
| 1 1 | 
             
            module NyanCatFormat
         | 
| 2 2 | 
             
              module Music
         | 
| 3 3 |  | 
| 4 | 
            -
                MUSIC_LENGTH = 27.06 # seconds
         | 
| 5 | 
            -
             | 
| 6 4 | 
             
                def osx?
         | 
| 7 5 | 
             
                  platform.downcase.include?("darwin")
         | 
| 8 6 | 
             
                end
         | 
| @@ -33,30 +31,8 @@ module NyanCatFormat | |
| 33 31 |  | 
| 34 32 | 
             
                def start input
         | 
| 35 33 | 
             
                  super
         | 
| 36 | 
            -
                   | 
| 37 | 
            -
             | 
| 38 | 
            -
                      if osx?
         | 
| 39 | 
            -
                        kernel.system("afplay #{nyan_mp3} &")
         | 
| 40 | 
            -
                      elsif linux?
         | 
| 41 | 
            -
                        play_on_linux
         | 
| 42 | 
            -
                      end
         | 
| 43 | 
            -
                      Thread.current["started_playing"] ||= true
         | 
| 44 | 
            -
                      sleep MUSIC_LENGTH
         | 
| 45 | 
            -
                    end
         | 
| 46 | 
            -
                  end
         | 
| 47 | 
            -
                  until t["started_playing"]
         | 
| 48 | 
            -
                    sleep 0.001
         | 
| 49 | 
            -
                  end
         | 
| 50 | 
            -
                end
         | 
| 51 | 
            -
             | 
| 52 | 
            -
                def kill_music
         | 
| 53 | 
            -
                  if File.exists? nyan_mp3
         | 
| 54 | 
            -
                    if osx?
         | 
| 55 | 
            -
                      system("killall -9 afplay &>/dev/null")
         | 
| 56 | 
            -
                    elsif linux?
         | 
| 57 | 
            -
                      kill_music_on_linux
         | 
| 58 | 
            -
                    end
         | 
| 59 | 
            -
                  end
         | 
| 34 | 
            +
                  @music_thread = Thread.new { start_music_or_kill(Thread.current) }
         | 
| 35 | 
            +
                  wait_for_music_to_start(@music_thread)
         | 
| 60 36 | 
             
                end
         | 
| 61 37 |  | 
| 62 38 | 
             
                def dump_summary(*args)
         | 
| @@ -66,15 +42,42 @@ module NyanCatFormat | |
| 66 42 |  | 
| 67 43 | 
             
                private
         | 
| 68 44 |  | 
| 69 | 
            -
                def  | 
| 70 | 
            -
                   | 
| 71 | 
            -
             | 
| 45 | 
            +
                def kill_music
         | 
| 46 | 
            +
                  if @music_thread && @music_thread['music_pid']
         | 
| 47 | 
            +
                    @music_thread.kill
         | 
| 48 | 
            +
                    Process.kill('KILL', @music_thread['music_pid'])
         | 
| 49 | 
            +
                  end
         | 
| 72 50 | 
             
                end
         | 
| 73 51 |  | 
| 74 | 
            -
                def  | 
| 75 | 
            -
                   | 
| 76 | 
            -
             | 
| 52 | 
            +
                def linux_player
         | 
| 53 | 
            +
                  %w{mpg321 mpg123}.find {|player|
         | 
| 54 | 
            +
                    kernel.system("which #{ player } &>/dev/null && type #{ player } &>/dev/null")
         | 
| 55 | 
            +
                  }
         | 
| 77 56 | 
             
                end
         | 
| 78 57 |  | 
| 58 | 
            +
                def music_command
         | 
| 59 | 
            +
                  # this isn't really threadsafe but it'll work if we're careful
         | 
| 60 | 
            +
                  return @music_command if @music_command
         | 
| 61 | 
            +
                  if osx?
         | 
| 62 | 
            +
                    @music_command = "afplay #{nyan_mp3}"
         | 
| 63 | 
            +
                  elsif linux? && linux_player
         | 
| 64 | 
            +
                    @music_command = "#{ linux_player } #{ nyan_mp3 } &>/dev/null"
         | 
| 65 | 
            +
                  end
         | 
| 66 | 
            +
                end
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                def start_music_or_kill(thread)
         | 
| 69 | 
            +
                  thread.exit unless File.exists?(nyan_mp3) && music_command
         | 
| 70 | 
            +
                  loop do
         | 
| 71 | 
            +
                    thread['music_pid'] = kernel.spawn(music_command)
         | 
| 72 | 
            +
                    thread["started_playing"] ||= true
         | 
| 73 | 
            +
                    Process.wait(thread['music_pid'])
         | 
| 74 | 
            +
                  end
         | 
| 75 | 
            +
                end
         | 
| 76 | 
            +
             | 
| 77 | 
            +
                def wait_for_music_to_start(music_thread)
         | 
| 78 | 
            +
                  while !music_thread["started_playing"] && music_thread.status
         | 
| 79 | 
            +
                    sleep 0.001
         | 
| 80 | 
            +
                  end
         | 
| 81 | 
            +
                end
         | 
| 79 82 | 
             
              end
         | 
| 80 83 | 
             
            end
         | 
    
        data/nyan-cat-formatter.gemspec
    CHANGED
    
    | @@ -2,7 +2,7 @@ $:.push File.expand_path("../lib", __FILE__) | |
| 2 2 |  | 
| 3 3 | 
             
            Gem::Specification.new do |s|
         | 
| 4 4 | 
             
              s.name        = "nyan-cat-formatter"
         | 
| 5 | 
            -
              s.version     = "0. | 
| 5 | 
            +
              s.version     = "0.9.0"
         | 
| 6 6 | 
             
              s.authors     = ["Matt Sears"]
         | 
| 7 7 | 
             
              s.email       = ["matt@mattsears.com"]
         | 
| 8 8 | 
             
              s.homepage    = "https://github.com/mattsears/nyan-cat-formatter"
         | 
| @@ -1,5 +1,6 @@ | |
| 1 1 | 
             
            require 'spec_helper'
         | 
| 2 2 | 
             
            require 'stringio'
         | 
| 3 | 
            +
            require 'fileutils'
         | 
| 3 4 | 
             
            require 'nyan_cat_music_formatter'
         | 
| 4 5 |  | 
| 5 6 | 
             
            class MockKernel
         | 
| @@ -7,6 +8,11 @@ class MockKernel | |
| 7 8 | 
             
                seen << string
         | 
| 8 9 | 
             
              end
         | 
| 9 10 |  | 
| 11 | 
            +
              def spawn(string)
         | 
| 12 | 
            +
                seen << string
         | 
| 13 | 
            +
                rand(10000)
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
             | 
| 10 16 | 
             
              def seen
         | 
| 11 17 | 
             
                @seen ||= []
         | 
| 12 18 | 
             
              end
         | 
| @@ -45,6 +51,11 @@ describe NyanCatMusicFormatter do | |
| 45 51 | 
             
              end
         | 
| 46 52 |  | 
| 47 53 | 
             
              describe 'start' do
         | 
| 54 | 
            +
                before do
         | 
| 55 | 
            +
                  allow(Process).to receive(:wait) { sleep 1 }
         | 
| 56 | 
            +
                  allow(Process).to receive(:kill).and_return(true)
         | 
| 57 | 
            +
                end
         | 
| 58 | 
            +
             | 
| 48 59 | 
             
                it 'sets the total amount of specs' do
         | 
| 49 60 | 
             
                  formatter.start 3
         | 
| 50 61 | 
             
                  expect(formatter.example_count).to eql(3)
         | 
| @@ -60,20 +71,29 @@ describe NyanCatMusicFormatter do | |
| 60 71 |  | 
| 61 72 | 
             
                  it 'plays the song in the background' do
         | 
| 62 73 | 
             
                    formatter.start 3
         | 
| 63 | 
            -
                    expect(mock_kernel.seen).to include("afplay #{path_to_mp3} | 
| 74 | 
            +
                    expect(mock_kernel.seen).to include("afplay #{path_to_mp3}")
         | 
| 64 75 | 
             
                  end
         | 
| 65 76 | 
             
                end
         | 
| 66 77 |  | 
| 67 78 | 
             
                context 'when on linux' do
         | 
| 68 79 | 
             
                  before { formatter.platform = 'linux'}
         | 
| 69 | 
            -
             | 
| 80 | 
            +
             | 
| 81 | 
            +
                  it 'plays the song for linux too with mpg123 when available' do
         | 
| 82 | 
            +
                    allow(mock_kernel).to receive(:system).with(match(/which mpg123/)).and_return(true)
         | 
| 83 | 
            +
                    allow(mock_kernel).to receive(:system).with(match(/which mpg321/)).and_return(false)
         | 
| 84 | 
            +
                    formatter.start 10
         | 
| 85 | 
            +
                    expect(mock_kernel.seen.any? { |entry| entry. end_with? "mpg123 #{path_to_mp3} &>/dev/null" }).to be
         | 
| 86 | 
            +
                  end
         | 
| 87 | 
            +
             | 
| 88 | 
            +
                  it 'plays the song for linux too with mpg321 when available' do
         | 
| 89 | 
            +
                    allow(mock_kernel).to receive(:system).with(match(/which mpg321/)).and_return(true)
         | 
| 90 | 
            +
                    allow(mock_kernel).to receive(:system).with(match(/which mpg123/)).and_return(false)
         | 
| 70 91 | 
             
                    formatter.start 10
         | 
| 71 | 
            -
                    expect(mock_kernel.seen.any? { |entry| entry. end_with? "mpg321 #{path_to_mp3} &>/dev/null | 
| 72 | 
            -
                    expect(mock_kernel.seen.any? { |entry| entry. end_with? "mpg123 #{path_to_mp3} &>/dev/null &" }).to be
         | 
| 92 | 
            +
                    expect(mock_kernel.seen.any? { |entry| entry. end_with? "mpg321 #{path_to_mp3} &>/dev/null" }).to be
         | 
| 73 93 | 
             
                  end
         | 
| 74 94 | 
             
                end
         | 
| 75 95 |  | 
| 76 | 
            -
                context 'when  | 
| 96 | 
            +
                context 'when on Windows' do
         | 
| 77 97 | 
             
                  before { formatter.platform = 'windows' }
         | 
| 78 98 |  | 
| 79 99 | 
             
                  it 'does not play the song' do
         | 
| @@ -81,5 +101,20 @@ describe NyanCatMusicFormatter do | |
| 81 101 | 
             
                    expect(mock_kernel.seen).to be_empty
         | 
| 82 102 | 
             
                  end
         | 
| 83 103 | 
             
                end
         | 
| 104 | 
            +
             | 
| 105 | 
            +
                context 'when the music file does not exist' do
         | 
| 106 | 
            +
                  before do
         | 
| 107 | 
            +
                    FileUtils.mv path_to_mp3, "#{ path_to_mp3 }.tmp"
         | 
| 108 | 
            +
                  end
         | 
| 109 | 
            +
             | 
| 110 | 
            +
                  after do
         | 
| 111 | 
            +
                    FileUtils.mv "#{ path_to_mp3 }.tmp", path_to_mp3
         | 
| 112 | 
            +
                  end
         | 
| 113 | 
            +
             | 
| 114 | 
            +
                  it "won't try to play anything" do
         | 
| 115 | 
            +
                    formatter.start 42
         | 
| 116 | 
            +
                    expect(mock_kernel.seen).to be_empty
         | 
| 117 | 
            +
                  end
         | 
| 118 | 
            +
                end
         | 
| 84 119 | 
             
              end
         | 
| 85 120 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: nyan-cat-formatter
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.9.0
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2014-06- | 
| 12 | 
            +
            date: 2014-06-15 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: rspec
         |