1#!/usr/bin/perl
2
3# Z88DK Z80 Module Assembler
4#
5# Copyright (C) Paulo Custodio, 2011-2020
6# License: The Artistic License 2.0, http://www.perlfoundation.org/artistic_license_2_0
7# Repository: https://github.com/z88dk/z88dk/
8#
9# Test source lists (@files)
10
11use Modern::Perl;
12use Test::More;
13use File::Path 'make_path';
14require './t/testlib.pl';
15
16make_test_files();
17run('z80asm -b test1.asm test2.asm test3.asm test4.asm');
18check_bin_file("test1.bin", pack("C*", 1..4));
19
20# list file with blank lines and comments
21make_test_files();
22spew("test1.lst", <<'END');
23; comment followed by blank line
24
25# comment
26   test2.asm
27@  test2.lst
28END
29
30# list file with different EOL chars
31spew("test2.lst",
32	"\r\r\n\n  ".
33	"test3.asm".
34	"  \r\r\n\n    ".
35	"test4.asm".
36	"\n");
37
38run('z80asm -b test1.asm "@test1.lst"');
39check_bin_file("test1.bin", pack("C*", 1..4));
40
41# recursive includes
42make_test_files();
43spew("test1.lst",
44	"\r\r\n\n  ".
45	"test2.asm".
46	"  \r\r\n\n  \@ ".
47	"test2.lst");
48
49spew("test2.lst",
50	"\r\r\n\n  ".
51	"test2.asm".
52	"  \r\r\n\n  \@ ".
53	"test1.lst");
54run('z80asm -b test1.asm "@test1.lst"', 1, "", <<'ERR');
55Error at file 'test2.lst' line 7: cannot include file 'test1.lst' recursively
56ERR
57
58# expand environment variables in source and list files
59make_test_files();
60$ENV{TEST_ENV} = 'test';
61
62run('z80asm -b "${TEST_ENV}1.asm" "${TEST_ENV}2.asm" "${TEST_ENV}3.asm" "${TEST_ENV}4.asm"');
63check_bin_file("test1.bin", pack("C*", 1..4));
64
65make_test_files();
66spew("test1.lst", <<'END');
67  ${TEST_ENV}1.asm
68  ${TEST_ENV}2.asm
69
70# see #440
71@ ${TEST_ENV}2.lst
72END
73
74spew("test2.lst", <<'END');
75  ${TEST_ENV}3.asm
76  ${TEST_ENV}4.asm
77END
78
79run('z80asm -b "@test1.lst"');
80check_bin_file("test1.bin", pack("C*", 1..4));
81
82# non-existent environment variable is empty
83delete $ENV{TEST_ENV};
84
85make_test_files();
86run('z80asm -b "te${TEST_ENV}st1.asm" "te${TEST_ENV}st2.asm" "te${TEST_ENV}st3.asm" "te${TEST_ENV}st4.asm"');
87check_bin_file("test1.bin", pack("C*", 1..4));
88
89make_test_files();
90spew("test1.lst", <<'END');
91  te${TEST_ENV}st1.asm
92  te${TEST_ENV}st2.asm
93
94# see #440
95@ te${TEST_ENV}st2.lst
96END
97
98spew("test2.lst", <<'END');
99  te${TEST_ENV}st3.asm
100  te${TEST_ENV}st4.asm
101END
102
103run('z80asm -b "@test1.lst"');
104check_bin_file("test1.bin", pack("C*", 1..4));
105
106# use globs in command line
107# Note: only relevant for Windows as Unix expands the command line
108# before calling the command
109make_test_files("test_dir");
110run('z80asm -b "test_dir/*.asm"');
111check_bin_file("test_dir/test1.bin", pack("C*", 1..4));
112
113# use globs in list file
114make_test_files("test_dir");
115spew("test1.lst", <<'END');
116	test_dir/*.asm
117END
118run('z80asm -b "@test1.lst"');
119check_bin_file("test_dir/test1.bin", pack("C*", 1..4));
120
121# error if no files are returned
122unlink_testfiles();
123mkdir("test_dir");
124spew("test1.lst", <<'END');
125	test_dir/*.asm
126END
127run('z80asm -b "@test1.lst"', 1, "", <<'ERR');
128Error at file 'test1.lst' line 1: pattern 'test_dir/*.asm' returned no files
129ERR
130
131# use globs in recursive list file name
132make_test_files("test_dir");
133spew("test1.lst", <<'END');
134	@ test_dir/*.lst
135END
136for (1..4) {
137	spew("test_dir/test$_.lst", "test_dir/test$_.asm");
138}
139run('z80asm -b "@test1.lst"');
140check_bin_file("test_dir/test1.bin", pack("C*", 1..4));
141
142# use ** glob for any number of directories
143unlink_testfiles();
144mkdir("test_dir");
145for (1..4) {
146	my $dir = "test_dir/$_/a/b";
147	make_path($dir);
148	spew("$dir/test$_.asm", "defb $_");
149	ok -f "$dir/test$_.asm", "create $dir/test$_.asm"
150}
151spew("test1.lst", <<'END');
152	test_dir/**/*.asm
153END
154run('z80asm -b "@test1.lst"');
155check_bin_file("test_dir/1/a/b/test1.bin", pack("C*", 1..4));
156
157# run again, .o files are not read as asm
158run('z80asm -b "@test1.lst"');
159check_bin_file("test_dir/1/a/b/test1.bin", pack("C*", 1..4));
160
161unlink_testfiles();
162done_testing();
163
164sub make_test_files {
165	my($dir) = @_;
166	$dir ||= ".";
167
168	unlink_testfiles();
169	mkdir($dir);
170	for (1..4) {
171		spew("$dir/test$_.asm", "defb $_");
172		ok -f "$dir/test$_.asm", "create $dir/test$_.asm"
173	}
174}
175