ref: 43890aec4916d92fd9f40b523d1024c824ebb7ac dhall-ruby/test/test_resolve.rb -rw-r--r-- 10.1 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# frozen_string_literal: true

require "base64"
require "securerandom"
require "webmock/minitest"
require "minitest/autorun"

require "dhall"

class TestResolve < Minitest::Test
	def setup
		@relative_to = Dhall::Import::RelativePath.new
		@resolver = Dhall::Resolvers::Default.new(
			path_reader:        lambda do |sources|
				sources.map do |source|
					Promise.resolve(Base64.decode64({
						"var"      => "AA",
						"import"   => "hRgY9gADY3Zhcg",
						"a"        => "hRgY9gADYWI",
						"b"        => "hRgY9gADYWE",
						"self"     => "hRgY9gADZHNlbGY",
						"text"     => "aGFp",
						"moretext" => "hRgY9gEDZHRleHQ",
						"2text"    => "hAMGhRgY9gEDZHRleHSFGBj2AANobW9yZXRleHQ",
						"using"    => "iBgY9gAAhRgY9gADZ2hlYWRlcnNkZS50ZGF09g",
						"headers"  => "gwT2ggiiZmhlYWRlcoISYnRoZXZhbHVlghJidHY"
					}.fetch(source.pathname.to_s)))
				end
			end,
			environment_reader: lambda do |sources|
				sources.map do |source|
					Promise.resolve({
						"NAT"  => "1",
						"PATH" => "./var"
					}.fetch(source.var))
				end
			end
		)
	end

	def subject(expr)
		expr.resolve(resolver: @resolver, relative_to: @relative_to).sync
	end

	def test_nothing_to_resolve
		expr = Dhall::Function.of_arguments(
			Dhall::Variable["Natural"],
			body: Dhall::Variable["_"]
		)

		assert_equal expr, expr.resolve(resolver: Dhall::Resolvers::None.new).sync
	end

	def test_import_as_text
		expr = Dhall::Import.new(
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Text,
			Dhall::Import::RelativePath.new("text")
		)

		assert_equal Dhall::Text.new(value: "hai"), subject(expr)
	end

	def test_one_level_to_resolve
		expr = Dhall::Function.of_arguments(
			Dhall::Variable["Natural"],
			body: Dhall::Import.new(
				Dhall::Import::NoIntegrityCheck.new,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("var")
			)
		)

		assert_equal expr.with(body: Dhall::Variable["_"]), subject(expr)
	end

	def test_two_levels_to_resolve
		expr = Dhall::Function.of_arguments(
			Dhall::Variable["Natural"],
			body: Dhall::Import.new(
				Dhall::Import::NoIntegrityCheck.new,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("import")
			)
		)

		assert_equal expr.with(body: Dhall::Variable["_"]), subject(expr)
	end

	def test_self_loop
		expr = Dhall::Function.of_arguments(
			Dhall::Variable["Natural"],
			body: Dhall::Import.new(
				Dhall::Import::NoIntegrityCheck.new,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("self")
			)
		)

		assert_raises Dhall::ImportLoopException do
			subject(expr)
		end
	end

	def test_two_level_loop
		expr = Dhall::Function.of_arguments(
			Dhall::Variable["Natural"],
			body: Dhall::Import.new(
				Dhall::Import::NoIntegrityCheck.new,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("a")
			)
		)

		assert_raises Dhall::ImportLoopException do
			subject(expr)
		end
	end

	def test_two_references_no_loop
		expr = Dhall::Import.new(
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Expression,
			Dhall::Import::RelativePath.new("2text")
		)

		assert_equal(
			Dhall::Operator::TextConcatenate.new(
				lhs: Dhall::Text.new(value: "hai"),
				rhs: Dhall::Text.new(value: "hai")
			),
			subject(expr)
		)
	end

	def test_forever_no_loop
		resolver = Dhall::Resolvers::LocalOnly.new(
			path_reader: lambda do |sources|
				sources.map do |_|
					Promise.resolve(nil).then do
						"./#{SecureRandom.hex}"
					end
				end
			end
		)

		assert_raises Dhall::ImportFailedException do
			Dhall.load(
				"./start",
				resolver: resolver,
				timeout:  Float::INFINITY
			).sync
		end
	end

	def test_missing
		expr = Dhall::Import.new(
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Expression,
			Dhall::Import::MissingImport.new
		)

		assert_raises Dhall::ImportFailedException do
			subject(expr)
		end
	end

	def test_integrity_check_failure
		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new(code: 0x12, digest: "badhash".b),
			Dhall::Import::Expression,
			Dhall::Import::RelativePath.new("var")
		)

		assert_raises Dhall::Import::IntegrityCheck::FailureException do
			subject(expr)
		end
	end

	def test_fallback_to_expr
		expr = Dhall::Operator::ImportFallback.new(
			lhs: Dhall::Import.new(
				Dhall::Import::NoIntegrityCheck.new,
				Dhall::Import::Expression,
				Dhall::Import::MissingImport.new
			),
			rhs: Dhall::Variable["fallback"]
		)

		assert_equal(
			Dhall::Variable["fallback"],
			subject(expr)
		)
	end

	def test_fallback_to_import
		expr = Dhall::Operator::ImportFallback.new(
			lhs: Dhall::Import.new(
				Dhall::Import::NoIntegrityCheck.new,
				Dhall::Import::Expression,
				Dhall::Import::MissingImport.new
			),
			rhs: Dhall::Import.new(
				Dhall::Import::NoIntegrityCheck.new,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("moretext")
			)
		)

		assert_equal Dhall::Text.new(value: "hai"), subject(expr)
	end

	def test_headers
		stub_request(:get, "http://e.td/t")
			.with(headers: { "Th" => "tv" })
			.to_return(status: 200, body: "\x00".b)

		expr = Dhall::Import.new(
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Expression,
			Dhall::Import::RelativePath.new("using")
		)

		assert_equal Dhall::Variable["_"], subject(expr)
	end

	def test_env_natural
		expr = Dhall::Import.new(
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Expression,
			Dhall::Import::EnvironmentVariable.new("NAT")
		)

		assert_equal Dhall::Natural.new(value: 1), subject(expr)
	end

	def test_env_as_text
		expr = Dhall::Import.new(
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Text,
			Dhall::Import::EnvironmentVariable.new("NAT")
		)

		assert_equal Dhall::Text.new(value: "1"), subject(expr)
	end

	def test_env_relative
		expr = Dhall::Import.new(
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Expression,
			Dhall::Import::EnvironmentVariable.new("PATH")
		)

		assert_equal Dhall::Variable["_"], subject(expr)
	end

	def test_integrity_check_ipfs
		check = Dhall::Import::IntegrityCheck.new(
			code:   0x12,
			digest: Dhall::Variable["_"].digest.digest
		)
		assert_equal(
			"/ipfs/bafkreidogqfzz75tpkmjzjke425xqcrmpcib2p5tg44hnbirumdbpl5adu",
			check.ipfs
		)
	end

	def test_cache
		req = stub_request(:get, "http://example.com/thing.dhall")
		      .to_return(status: 200, body: "\x00".b)

		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new(
				code:   0x12,
				digest: Dhall::Variable["_"].digest.digest
			),
			Dhall::Import::Expression,
			Dhall::Import::Http.new(uri: URI("http://example.com/thing.dhall"))
		)

		cache = Dhall::Resolvers::RamCache.new

		assert_equal(
			Dhall::Variable["_"],
			expr.resolve(
				resolver: Dhall::Resolvers::Default.new(cache: cache)
			).sync
		)

		assert_equal(
			Dhall::Variable["_"],
			expr.resolve(
				resolver: Dhall::Resolvers::Default.new(cache: cache)
			).sync
		)

		assert_requested(req, times: 1)
	end

	def test_ipfs
		stub_request(:get, "http://localhost:8000/ipfs/TESTCID")
			.to_return(status: 200, body: "\x00".b)

		expr = Dhall::Import.new(
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Expression,
			Dhall::Import::AbsolutePath.new("ipfs", "TESTCID")
		)

		assert_equal Dhall::Variable["_"], expr.resolve.sync
	end

	def test_ipfs_public_gateway
		stub_request(:get, "http://localhost:8000/ipfs/TESTCID")
			.to_return(status: 500)

		stub_request(:get, "https://cloudflare-ipfs.com/ipfs/TESTCID")
			.to_return(status: 200, body: "_")

		expr = Dhall::Import.new(
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Expression,
			Dhall::Import::AbsolutePath.new("ipfs", "TESTCID")
		)

		assert_equal Dhall::Variable["_"], expr.resolve.sync
	end

	DIRPATH = Pathname.new(File.dirname(__FILE__)).realpath
	TESTS = (DIRPATH + "../dhall-lang/tests/import/").realpath

	Pathname.glob(TESTS + "success/**/*A.dhall").each do |path|
		Dir.chdir(path.dirname)
		ENV["XDG_CACHE_HOME"] = (TESTS + "cache").to_s

		test = path.relative_path_from(TESTS).to_s.sub(/A\.dhall$/, "")

		define_method("test_#{test}") do
			stub_request(:any, "https://httpbin.org/user-agent")
				.to_return do |req|
					{
						headers: { "Access-Control-Allow-Origin": "*" },
						body:    <<~JSON
							{
							  "user-agent": "#{req.headers["User-Agent"]}"
							}
						JSON
					}
				end

			stub_request(
				:get,
				"https://raw.githubusercontent.com/dhall-lang/dhall-lang/" \
				"master/tests/import/success/customHeadersA.dhall"
			).to_return(body: (TESTS + "success/customHeadersA.dhall").read)

			stub_request(:get, "https://test.dhall-lang.org/Bool/package.dhall")
				.with(headers: { "Test" => "Example" })
				.to_return(body: "./test.dhall")

			stub_request(:get, "https://test.dhall-lang.org/Bool/test.dhall")
				.with(headers: { "Test" => "Example" })
				.to_return(body: (TESTS + "#{test}B.dhall").read)

			Dhall::Function.disable_alpha_normalization!
			assert_equal(
				Dhall::Parser.parse_file(TESTS + "#{test}B.dhall").value.normalize,
				Dhall::Parser.parse_file(path).value.resolve(
					resolver:    Dhall::Resolvers::Standard.new,
					relative_to: Dhall::Import::Path.from_string("./")
				).sync.normalize
			)
			Dhall::Function.enable_alpha_normalization!
		end
	end

	Pathname.glob(TESTS + "failure/**/*.dhall").each do |path|
		test = path.relative_path_from(TESTS).to_s.sub(/\.dhall$/, "")

		define_method("test_#{test}") do
			stub_request(
				:get,
				"https://raw.githubusercontent.com/dhall-lang/dhall-lang/" \
				"master/tests/import/data/referentiallyOpaque.dhall"
			).to_return(status: 200, body: "env:HOME as Text")

			e = if test =~ /hashMismatch/
				Dhall::Import::IntegrityCheck::FailureException
			else
				Dhall::ImportFailedException
			end
			assert_raises e do
				Dhall::Parser.parse_file(path).value.resolve(
					relative_to: Dhall::Import::Path.from_string(path)
				).sync
			end
		end
	end

	NTESTS = DIRPATH + "../dhall-lang/tests/normalization/"

	# Sanity check that all expressions can pass through the resolver
	Pathname.glob(NTESTS + "**/*A.dhall").each do |path|
		test = path.relative_path_from(TESTS).to_s.sub(/A\.dhall$/, "")
		next if test =~ /prelude\/|remoteSystems/

		define_method("test_#{test}") do
			expr = Dhall::Parser.parse_file(path).value
			assert_equal expr, expr.resolve.sync
		end
	end
end