Class: Bake::Test::External::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/bake/test/external/controller.rb

Constant Summary collapse

DEFAULT_COMMAND =
"bake test"
GEMFILE_NAMES =
["Gemfile", "gems.rb"]

Instance Method Summary collapse

Constructor Details

#initialize(root = nil) ⇒ Controller

Returns a new instance of Controller.



15
16
17
# File 'lib/bake/test/external/controller.rb', line 15

def initialize(root = nil)
	@root = Pathname.new(root || Dir.pwd)
end

Instance Method Details

#clone_and_test(name, key, config) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bake/test/external/controller.rb', line 35

def clone_and_test(name, key, config)
	$stderr.puts "Cloning external repository #{key}..."
	path = clone_repository(name, key, config)
	
	begin
		$stderr.puts "Running external tests #{key}..."
		test_repository(path, config)
	rescue
		$stderr.puts "External tests #{key} failed!"
		raise
	end
end

#clone_repository(name, key, config) ⇒ Object



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
# File 'lib/bake/test/external/controller.rb', line 48

def clone_repository(name, key, config)
	url = config[:url]
	
	path = File.join(@root, "external", key)
	
	unless File.directory?(path)
		FileUtils.mkdir_p path
		command = ["git", "clone", "--depth", "1"]
		
		if branch = config[:branch]
			command << "--branch" << branch
		end
		
		if tag = config[:tag]
			command << "--tag" << tag
		end
		
		command << url << path
		system!(config[:env], *command)
		
		# I tried using `bundle config --local local.async ../` but it simply doesn't work.
		# system!("bundle", "config", "--local", "local.async", __dir__, chdir: path)
		
		gemfile_path = self.gemfile_path(path, config)
		relative_root = @root.relative_path_from(gemfile_path.dirname)
		
		File.open(gemfile_path, "r+") do |file|
			pattern = /gem.*?['"]#{name}['"]/
			lines = file.grep_v(pattern)

			file.seek(0)
			file.truncate(0)
			file.puts(lines)
			file.puts nil, "# Added by external testing:"
			file.puts("gem #{name.to_s.dump}, path: #{relative_root.to_s.dump}")

			config[:extra]&.each do |line|
				file.puts(line)
			end
		end
		
		system!(config[:env], "bundle", "install", chdir: path)
	else
		# This also sets the config[:env]['BUNDLE_GEMFILE'] if necessary:
		self.gemfile_path(path, config)
	end
	
	return path
end

#find_gemspec(pattern = "*.gemspec") ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bake/test/external/controller.rb', line 23

def find_gemspec(pattern = "*.gemspec")
	paths = @root.glob(pattern)
	
	if paths.size > 1
		raise "Multiple gemspecs found: #{paths}, please specify one!"
	end
	
	if path = paths.first
		return ::Gem::Specification.load(path.to_s)
	end
end

#gemfile_path(root, config) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/bake/test/external/controller.rb', line 132

def gemfile_path(root, config)
	config.fetch(:cached_gemfile_path) do
		root = File.expand_path(root, @root)
		default, path = self.resolve_gemfile_path(root, config)
		
		# Custom gemfile paths should be set explicitly:
		unless default
			config[:env]["BUNDLE_GEMFILE"] = path
		end
		
		path = Pathname.new(path)
		
		config[:cached_gemfile_path] = path
		
		return path
	end
end

#resolve_gemfile_path(root, config) ⇒ Object

Raises:

  • (ArgumentError)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/bake/test/external/controller.rb', line 108

def resolve_gemfile_path(root, config)
	if config_path = config[:gemfile]
		path = File.join(root, config_path)
		
		unless File.exist?(path)
			raise ArgumentError, "Specified gemfile path does not exist: #{config_path.inspect}!"
		end
		
		# We consider this to be a custom gemfile path:
		return false, path
	end
	
	GEMFILE_NAMES.each do |name|
		path = File.join(root, name)
		
		if File.exist?(path)
			# We consider this to be a default gemfile path:
			return true, path
		end
	end
	
	raise ArgumentError, "Could not find gem file in #{root.inspect}!"
end

#test_repository(path, config) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/bake/test/external/controller.rb', line 98

def test_repository(path, config)
	command = config.fetch(:command, DEFAULT_COMMAND)
	
	Array(command).each do |line|
		system!(config[:env], *line, chdir: path)
	end
end