Skip to content

Commit 0c64b74

Browse files
authored
Fixed unit tests on Windows (Appveyor) (#1560)
* Fixed unit tests on Windows * Fixes #1559 * 🐛 ensure we always use expected line endings * Add a trailing blank line * More fixes
1 parent 8518a6b commit 0c64b74

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

pythonFiles/normalizeForInterpreter.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,38 +77,44 @@ def normalize_lines(source):
7777
"""
7878
lines = source.splitlines(False)
7979
# Find out if we have any trailing blank lines
80-
has_blank_lines = len(lines[-1].strip()) == 0 or source.endswith(os.linesep)
80+
if len(lines[-1].strip()) == 0 or source.endswith('\n'):
81+
trailing_newline = '\n'
82+
else:
83+
trailing_newline = ''
8184

8285
# Step 1: Remove empty lines.
8386
tokens = _tokenize(source)
8487
newlines_indexes_to_remove = (spos[0] for (toknum, tokval, spos, epos, line) in tokens
85-
if len(line.strip()) == 0 and token.tok_name[toknum] == 'NL' and spos[0] == epos[0])
88+
if len(line.strip()) == 0
89+
and token.tok_name[toknum] == 'NL'
90+
and spos[0] == epos[0])
8691

8792
for line_number in reversed(list(newlines_indexes_to_remove)):
8893
del lines[line_number-1]
8994

9095
# Step 2: Add blank lines between each global statement block.
9196
# A consequtive single lines blocks of code will be treated as a single statement,
9297
# just to ensure we do not unnecessarily add too many blank lines.
93-
source = os.linesep.join(lines)
98+
source = '\n'.join(lines)
9499
tokens = _tokenize(source)
95100
dedent_indexes = (spos[0] for (toknum, tokval, spos, epos, line) in tokens
96101
if toknum == token.DEDENT and _indent_size(line) == 0)
97102

98103
global_statement_ranges = _get_global_statement_blocks(source, lines)
99-
100-
for line_number in filter(lambda x: x > 1, map(operator.itemgetter(0), reversed(global_statement_ranges))):
104+
start_positions = map(operator.itemgetter(0), reversed(global_statement_ranges))
105+
for line_number in filter(lambda x: x > 1, start_positions):
101106
lines.insert(line_number-1, '')
102107

103-
sys.stdout.write(os.linesep.join(lines) + (os.linesep if has_blank_lines else ''))
108+
sys.stdout.write('\n'.join(lines) + trailing_newline)
104109
sys.stdout.flush()
105110

106111

107112
if __name__ == '__main__':
108113
contents = sys.argv[1]
109114
try:
110115
default_encoding = sys.getdefaultencoding()
111-
contents = contents.encode(default_encoding, 'surrogateescape').decode(default_encoding, 'replace')
116+
encoded_contents = contents.encode(default_encoding, 'surrogateescape')
117+
contents = encoded_contents.decode(default_encoding, 'replace')
112118
except (UnicodeError, LookupError):
113119
pass
114120
if isinstance(contents, bytes):

src/test/terminals/codeExecution/helper.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as TypeMoq from 'typemoq';
1111
import { Range, Selection, TextDocument, TextEditor, TextLine, Uri } from 'vscode';
1212
import { IApplicationShell, IDocumentManager } from '../../../client/common/application/types';
1313
import { EXTENSION_ROOT_DIR, PYTHON_LANGUAGE } from '../../../client/common/constants';
14+
import '../../../client/common/extensions';
1415
import { BufferDecoder } from '../../../client/common/process/decoder';
1516
import { ProcessService } from '../../../client/common/process/proc';
1617
import { IProcessService } from '../../../client/common/process/types';
@@ -62,6 +63,8 @@ suite('Terminal - Code Execution Helper', () => {
6263
return actualProcessService.exec.apply(actualProcessService, [file, args, options]);
6364
});
6465
const normalizedZCode = await helper.normalizeLines(source);
66+
// In case file has been saved with different line endings.
67+
expectedSource = expectedSource.splitLines({ removeEmptyEntries: false, trim: false }).join(EOL);
6568
expect(normalizedZCode).to.be.equal(expectedSource);
6669
}
6770
test('Ensure blank lines are NOT removed when code is not indented (simple)', async () => {

0 commit comments

Comments
 (0)