Skip to content

Commit

Permalink
Do not copy pseudo files (device, socket, etc.) in dynamic analysis
Browse files Browse the repository at this point in the history
Related to #17.
  • Loading branch information
yujinakayama committed Nov 10, 2013
1 parent 7332576 commit 018d3b5
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Development

* Fix error `singleton can't be dumped (TypeError)` at the end of dynamic analysis ([#17](https://github.com/yujinakayama/transpec/issues/17))
* Do not copy pseudo files (device, socket, etc.) in dynamic analysis ([#17](https://github.com/yujinakayama/transpec/issues/17))

## v1.2.1

Expand Down
38 changes: 37 additions & 1 deletion lib/transpec/dynamic_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
require 'transpec/file_finder'
require 'transpec/project'
require 'tmpdir'
require 'find'
require 'pathname'
require 'fileutils'
require 'shellwords'
require 'English'
Expand Down Expand Up @@ -127,7 +129,7 @@ def in_copied_project
@in_copied_project = true

Dir.mktmpdir do |tmpdir|
FileUtils.cp_r(@project.path, tmpdir)
copy_recursively(@project.path, tmpdir)
@copied_project_path = File.join(tmpdir, @project.basename)
Dir.chdir(@copied_project_path) do
yield
Expand Down Expand Up @@ -159,5 +161,39 @@ def run_rspec(paths)
end
end
end

def copy_recursively(source_root, destination_root)
source_root = File.expand_path(source_root)
source_root_pathname = Pathname.new(source_root)

destination_root = File.expand_path(destination_root)
if File.directory?(destination_root)
destination_root = File.join(destination_root, File.basename(source_root))
end

Find.find(source_root) do |source_path|
relative_path = Pathname.new(source_path).relative_path_from(source_root_pathname).to_s
destination_path = File.join(destination_root, relative_path)

copy(source_path, destination_path)

if File.exist?(destination_path)
source_mode = File.lstat(source_path).mode
File.lchmod(source_mode, destination_path)
end
end
end

private

def copy(source, destination)
if File.symlink?(source)
File.symlink(File.readlink(source), destination)
elsif File.directory?(source)
FileUtils.mkdir_p(destination)
elsif File.file?(source)
FileUtils.copy_file(source, destination)
end
end
end
end
58 changes: 58 additions & 0 deletions spec/transpec/dynamic_analyzer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,63 @@ module Transpec
end
end
end

describe '#copy_recursively' do
it 'copies files recursively' do
[
'src/file1',
'src/file2',
'src/dir1/file',
'src/dir2/file'
].each do |path|
create_file(path, '')
end

dynamic_analyzer.copy_recursively('src', 'dst')

[
'dst/file1',
'dst/file2',
'dst/dir1/file',
'dst/dir2/file'
].each do |path|
File.exist?(path).should be_true
end
end

it 'copies only directories, files and symlinks' do
create_file('src/file', '')
File.symlink('file', 'src/symlink')
Dir.mkdir('src/dir')
system('mkfifo', 'src/fifo')

dynamic_analyzer.copy_recursively('src', 'dst')

File.file?('dst/file').should be_true
File.symlink?('dst/symlink').should be_true
File.directory?('dst/dir').should be_true
File.exist?('dst/fifo').should be_false
end

def permission(path)
format('%o', File.lstat(path).mode)[-4..-1]
end

it 'preserves permission' do
create_file('src/file', '')
File.lchmod(0755, 'src/file')

File.symlink('file', 'src/symlink')
File.lchmod(0777, 'src/symlink')

Dir.mkdir('src/dir')
File.lchmod(0600, 'src/dir')
dynamic_analyzer.copy_recursively('src', 'dst')

permission('dst/file').should == '0755'
permission('dst/symlink').should == '0777'
permission('dst/dir').should == '0600'
end
end
end
end

0 comments on commit 018d3b5

Please sign in to comment.