Skip to content

Commit 9993c51

Browse files
authored
Run textwrap.dedent on text files created by the test runner (#21101)
This means we can indent out test snippets in the python code and should never need to use column 1. The files on disc that the test runner writes will have sane/normal indentation even if we use extra indentation in the python source to make things readable. For example: ``` create_file('a.c', ''' int main() printf("hello\n"); } ''') ``` Would generate a file on disc containing: ``` int main() printf("hello\n"); } ```
1 parent c00356e commit 9993c51

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

test/common.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import subprocess
2626
import sys
2727
import tempfile
28+
import textwrap
2829
import time
2930
import webbrowser
3031
import unittest
@@ -505,6 +506,10 @@ def create_file(name, contents, binary=False, absolute=False):
505506
if binary:
506507
name.write_bytes(contents)
507508
else:
509+
# Dedent the contents of text files so that the files on disc all
510+
# start in column 1, even if they are indented when embedded in the
511+
# python test code.
512+
contents = textwrap.dedent(contents)
508513
name.write_text(contents, encoding='utf-8')
509514

510515

test/test_other.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,24 +1240,24 @@ def build(path, args):
12401240

12411241
def test_commons_link(self):
12421242
create_file('a.h', r'''
1243-
#if !defined(A_H)
1244-
#define A_H
1245-
extern int foo[8];
1246-
#endif
1247-
''')
1243+
#if !defined(A_H)
1244+
#define A_H
1245+
extern int foo[8];
1246+
#endif
1247+
''')
12481248
create_file('a.c', r'''
1249-
#include "a.h"
1250-
int foo[8];
1251-
''')
1249+
#include "a.h"
1250+
int foo[8];
1251+
''')
12521252
create_file('main.c', r'''
1253-
#include <stdio.h>
1254-
#include "a.h"
1253+
#include <stdio.h>
1254+
#include "a.h"
12551255

1256-
int main() {
1257-
printf("|%d|\n", foo[0]);
1258-
return 0;
1259-
}
1260-
''')
1256+
int main() {
1257+
printf("|%d|\n", foo[0]);
1258+
return 0;
1259+
}
1260+
''')
12611261

12621262
self.run_process([EMCC, '-o', 'a.o', '-c', 'a.c'])
12631263
self.run_process([EMAR, 'rv', 'library.a', 'a.o'])

0 commit comments

Comments
 (0)