Auto merge of #126788 - GuillaumeGomez:migrate-rustdoc-tests-syntax, … · rust-lang/rust@06c072f (original) (raw)

`@@ -240,9 +240,37 @@ def concat_multi_lines(f):

`

240

240

`print_err(lineno, line, 'Trailing backslash at the end of the file')

`

241

241

``

242

242

``

``

243

`+

def get_known_directive_names():

`

``

244

`+

def filter_line(line):

`

``

245

`+

line = line.strip()

`

``

246

`+

return line.startswith('"') and (line.endswith('",') or line.endswith('"'))

`

``

247

+

``

248

`` +

Equivalent to src/tools/compiletest/src/header.rs constant of the same name.

``

``

249

`+

with open(

`

``

250

`+

os.path.join(

`

``

251

`` +

We go back to src.

``

``

252

`+

os.path.dirname(os.path.dirname(file)),

`

``

253

`+

"tools/compiletest/src/command-list.rs",

`

``

254

`+

),

`

``

255

`+

"r",

`

``

256

`+

encoding="utf8"

`

``

257

`+

) as fd:

`

``

258

`+

content = fd.read()

`

``

259

`+

return [

`

``

260

`+

line.strip().replace('",', '').replace('"', '')

`

``

261

`+

for line in content.split('\n')

`

``

262

`+

if filter_line(line)

`

``

263

`+

]

`

``

264

+

``

265

+

``

266

`` +

To prevent duplicating the list of commmands between compiletest and htmldocck, we put

``

``

267

`+

it into a common file which is included in rust code and parsed here.

`

``

268

`+

FIXME: This setup is temporary until we figure out how to improve this situation.

`

``

269

`+

KNOWN_DIRECTIVE_NAMES = get_known_directive_names()

`

``

270

+

243

271

`LINE_PATTERN = re.compile(r'''

`

244

``

`-

(?<=(?<!\S))(?P!?)@(?P!?)

`

245

``

`-

(?P[A-Za-z]+(?:-[A-Za-z]+)*)

`

``

272

`+

//@\s+

`

``

273

`+

(?P!?)(?P[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*)

`

246

274

` (?P.*)$

`

247

275

`''', re.X | re.UNICODE)

`

248

276

``

`@@ -254,17 +282,9 @@ def get_commands(template):

`

254

282

`if not m:

`

255

283

`continue

`

256

284

``

257

``

`-

negated = (m.group('negated') == '!')

`

258

285

`cmd = m.group('cmd')

`

259

``

`-

if m.group('invalid') == '!':

`

260

``

`-

print_err(

`

261

``

`-

lineno,

`

262

``

`-

line,

`

263

``

`` -

'Invalid command: !@{0}{1}, (help: try with @!{1})'.format(

``

264

``

`-

'!' if negated else '',

`

265

``

`-

cmd,

`

266

``

`-

),

`

267

``

`-

)

`

``

286

`+

negated = (m.group('negated') == '!')

`

``

287

`+

if not negated and cmd in KNOWN_DIRECTIVE_NAMES:

`

268

288

`continue

`

269

289

`args = m.group('args')

`

270

290

`if args and not args[:1].isspace():

`

`@@ -549,7 +569,7 @@ def get_nb_matching_elements(cache, c, regexp, stop_at_first):

`

549

569

`def check_files_in_folder(c, cache, folder, files):

`

550

570

`files = files.strip()

`

551

571

`if not files.startswith('[') or not files.endswith(']'):

`

552

``

`-

raise InvalidCheck("Expected list as second argument of @{} (ie '[]')".format(c.cmd))

`

``

572

`+

raise InvalidCheck("Expected list as second argument of {} (ie '[]')".format(c.cmd))

`

553

573

``

554

574

`folder = cache.get_absolute_path(folder)

`

555

575

``

`@@ -558,7 +578,7 @@ def check_files_in_folder(c, cache, folder, files):

`

558

578

`files_set = set()

`

559

579

`for file in files:

`

560

580

`if file in files_set:

`

561

``

`` -

raise InvalidCheck("Duplicated file {} in @{}".format(file, c.cmd))

``

``

581

`` +

raise InvalidCheck("Duplicated file {} in {}".format(file, c.cmd))

``

562

582

`files_set.add(file)

`

563

583

`folder_set = set([f for f in os.listdir(folder) if f != "." and f != ".."])

`

564

584

``

`@@ -590,48 +610,48 @@ def check_command(c, cache):

`

590

610

`if c.cmd in ['has', 'hasraw', 'matches', 'matchesraw']: # string test

`

591

611

`regexp = c.cmd.startswith('matches')

`

592

612

``

593

``

`-

@has = file existence

`

``

613

`+

has = file existence

`

594

614

`if len(c.args) == 1 and not regexp and 'raw' not in c.cmd:

`

595

615

`try:

`

596

616

`cache.get_file(c.args[0])

`

597

617

`ret = True

`

598

618

`except FailedCheck as err:

`

599

619

`cerr = str(err)

`

600

620

`ret = False

`

601

``

`-

@hasraw/matchesraw = string test

`

``

621

`+

hasraw/matchesraw = string test

`

602

622

`elif len(c.args) == 2 and 'raw' in c.cmd:

`

603

623

`` cerr = "PATTERN did not match"

``

604

624

`ret = check_string(cache.get_file(c.args[0]), c.args[1], regexp)

`

605

``

`-

@has/matches = XML tree test

`

``

625

`+

has/matches = XML tree test

`

606

626

`elif len(c.args) == 3 and 'raw' not in c.cmd:

`

607

627

`` cerr = "XPATH PATTERN did not match"

``

608

628

`ret = get_nb_matching_elements(cache, c, regexp, True) != 0

`

609

629

`else:

`

610

``

`-

raise InvalidCheck('Invalid number of @{} arguments'.format(c.cmd))

`

``

630

`+

raise InvalidCheck('Invalid number of {} arguments'.format(c.cmd))

`

611

631

``

612

632

`elif c.cmd == 'files': # check files in given folder

`

613

``

`-

if len(c.args) != 2: # @files

`

614

``

`-

raise InvalidCheck("Invalid number of @{} arguments".format(c.cmd))

`

``

633

`+

if len(c.args) != 2: # files

`

``

634

`+

raise InvalidCheck("Invalid number of {} arguments".format(c.cmd))

`

615

635

`elif c.negated:

`

616

``

`-

raise InvalidCheck("@{} doesn't support negative check".format(c.cmd))

`

``

636

`+

raise InvalidCheck("{} doesn't support negative check".format(c.cmd))

`

617

637

`ret = check_files_in_folder(c, cache, c.args[0], c.args[1])

`

618

638

``

619

639

`elif c.cmd == 'count': # count test

`

620

``

`-

if len(c.args) == 3: # @count = count test

`

``

640

`+

if len(c.args) == 3: # count = count test

`

621

641

`expected = int(c.args[2])

`

622

642

`found = get_tree_count(cache.get_tree(c.args[0]), c.args[1])

`

623

643

`cerr = "Expected {} occurrences but found {}".format(expected, found)

`

624

644

`ret = expected == found

`

625

``

`-

elif len(c.args) == 4: # @count = count test

`

``

645

`+

elif len(c.args) == 4: # count = count test

`

626

646

`expected = int(c.args[3])

`

627

647

`found = get_nb_matching_elements(cache, c, False, False)

`

628

648

`cerr = "Expected {} occurrences but found {}".format(expected, found)

`

629

649

`ret = found == expected

`

630

650

`else:

`

631

``

`-

raise InvalidCheck('Invalid number of @{} arguments'.format(c.cmd))

`

``

651

`+

raise InvalidCheck('Invalid number of {} arguments'.format(c.cmd))

`

632

652

``

633

653

`elif c.cmd == 'snapshot': # snapshot test

`

634

``

`-

if len(c.args) == 3: # @snapshot

`

``

654

`+

if len(c.args) == 3: # snapshot

`

635

655

` [snapshot_name, html_path, pattern] = c.args

`

636

656

`tree = cache.get_tree(html_path)

`

637

657

`xpath = normalize_xpath(pattern)

`

`@@ -654,33 +674,33 @@ def check_command(c, cache):

`

654

674

`else:

`

655

675

`raise FailedCheck('Expected 1 match, but found {}'.format(len(subtrees)))

`

656

676

`else:

`

657

``

`-

raise InvalidCheck('Invalid number of @{} arguments'.format(c.cmd))

`

``

677

`+

raise InvalidCheck('Invalid number of {} arguments'.format(c.cmd))

`

658

678

``

659

679

`elif c.cmd == 'has-dir': # has-dir test

`

660

``

`-

if len(c.args) == 1: # @has-dir = has-dir test

`

``

680

`+

if len(c.args) == 1: # has-dir = has-dir test

`

661

681

`try:

`

662

682

`cache.get_dir(c.args[0])

`

663

683

`ret = True

`

664

684

`except FailedCheck as err:

`

665

685

`cerr = str(err)

`

666

686

`ret = False

`

667

687

`else:

`

668

``

`-

raise InvalidCheck('Invalid number of @{} arguments'.format(c.cmd))

`

``

688

`+

raise InvalidCheck('Invalid number of {} arguments'.format(c.cmd))

`

669

689

``

670

690

`elif c.cmd == 'valid-html':

`

671

``

`-

raise InvalidCheck('Unimplemented @valid-html')

`

``

691

`+

raise InvalidCheck('Unimplemented valid-html')

`

672

692

``

673

693

`elif c.cmd == 'valid-links':

`

674

``

`-

raise InvalidCheck('Unimplemented @valid-links')

`

``

694

`+

raise InvalidCheck('Unimplemented valid-links')

`

675

695

``

676

696

`else:

`

677

``

`-

raise InvalidCheck('Unrecognized @{}'.format(c.cmd))

`

``

697

`+

raise InvalidCheck('Unrecognized {}'.format(c.cmd))

`

678

698

``

679

699

`if ret == c.negated:

`

680

700

`raise FailedCheck(cerr)

`

681

701

``

682

702

`except FailedCheck as err:

`

683

``

`-

message = '@{}{} check failed'.format('!' if c.negated else '', c.cmd)

`

``

703

`+

message = '{}{} check failed'.format('!' if c.negated else '', c.cmd)

`

684

704

`print_err(c.lineno, c.context, str(err), message)

`

685

705

`except InvalidCheck as err:

`

686

706

`print_err(c.lineno, c.context, str(err))

`