sapling/eden/scm/tests/test-dott-translate.py
Jun Wu 2a99866d7b codemod: update license headers
Summary:
The "Portions" license cannot be updated automatically. So this is a manual
update using:

  sd -s 'Portions Copyright (c) Facebook, Inc. and its affiliates.' 'Portions Copyright (c) Meta Platforms, Inc. and affiliates.' `rg -l Facebook`
  sd -s 'Copyright (c) Facebook, Inc. and its affiliates.' 'Copyright (c) Meta Platforms, Inc. and affiliates.' `rg -l Facebook`

Differential Revision: D33420114

fbshipit-source-id: 49ae00a7b62e3b8cc6c5dd839b3c104a75e72a56
2022-01-05 14:43:32 -08:00

158 lines
2.5 KiB
Python

# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from __future__ import absolute_import
from testutil.autofix import eq
from testutil.dott import feature
from testutil.dott.translate import translatebody
def translateeq(code, expected):
eq(translatebody(code + "\n").strip(), expected, nested=1)
# comments
translateeq("foo", "# foo\n")
translateeq("# foo", "# foo\n")
translateeq(
"A\n B\nC",
r"""
# A
# B
# C
""",
)
# shell functions
translateeq(" $ echo 'foo bar' baz > a.txt", "sh % 'echo \"foo bar\" baz' > 'a.txt'")
translateeq(
r"""
$ cat p.txt < a.txt \
> >> b.txt""",
"sh % 'cat p.txt' << open('a.txt').read() >> 'b.txt'",
)
translateeq(
r""" $ true && false ; false""",
r"""
sh % 'true'
sh % 'false'
sh % 'false'""",
)
# testing output
translateeq(
r"""
$ echo foo bar
foo bar
$ cat a.txt
a
b
""",
r'''
sh % 'echo foo bar' == 'foo bar'
sh % 'cat a.txt' == r"""
a
b"""''',
)
# heredoc as input
translateeq(
r"""
$ cat >> hgrc << EOF
> [ui]
> editor = foo
> EOF
""",
r'''
sh % 'cat' << r"""
[ui]
editor = foo
""" >> 'hgrc' ''',
)
# inline python
translateeq(
r"""
>>> for i in [1, 2]:
... print(i)
""",
r"""
for i in [1, 2]:
print(i)""",
)
# pipe
translateeq(" $ seq 1 10 | tail -2 >> b.txt", "sh % 'seq 1 10' | 'tail -2' >> 'b.txt'")
# FIXME: shell functions are not translated correctly
translateeq(
r"""
$ foo() {
> hg commit -m "$1"
> echo "$2"
> }
""",
r'''
sh % '"foo()" "{"' == r"""
> hg commit -m "$1"
> echo "$2"
> }"""''',
)
# FIXME: for loops are not translated correctly
translateeq(
r"""
$ for i in a b c; do
> hg commit -m $i
> done
""",
r'''
sh % 'for i in a b "c;" do' == r"""
> hg commit -m $i
> done"""''',
)
# '#if', '#require', '#testcases'
translateeq(
r"""
#if symlink
$ ln -s a b
#endif
$ echo after
""",
r"""
if feature.check(['symlink']):
sh % 'ln -s a b'
sh % 'echo after'""",
)
translateeq(
r"""
#require symlink execbit
""",
"feature.require(['symlink', 'execbit'])",
)
translateeq(
r"""
#testcases a b c
#if a
$ echo a
#else
$ echo b
#endif
""",
r"""
for testcase in ['a', 'b', 'c']:
if feature.check(['a']):
sh % 'echo a'
else:
sh % 'echo b'""",
)