From 5e27e8b982124848074427ef02d5c71efa262b08 Mon Sep 17 00:00:00 2001 From: Andy C Date: Wed, 27 Nov 2024 11:06:53 -0500 Subject: [PATCH 1/2] [osh/split] Test cases for SplitForWordEval Aidan noticed the issue that \ is treated as a special case. That is on for SplitForWordEval, but not necessarily SplitForRead. That seems to be 'read -r'. --- osh/split.py | 8 ++++++++ osh/split_test.py | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/osh/split.py b/osh/split.py index 72514befb7..912e7f820a 100644 --- a/osh/split.py +++ b/osh/split.py @@ -183,6 +183,10 @@ def SplitForWordEval(self, s, ifs=None): """ sp = self._GetSplitter(ifs=ifs) spans = sp.Split(s, True) + + # Note: pass allow_escape=False so \ isn't special + #spans = sp.Split(s, False) + if 0: for span in spans: log('SPAN %s', span) @@ -221,6 +225,10 @@ def __init__(self, ifs_whitespace, ifs_other): self.ifs_whitespace = ifs_whitespace self.ifs_other = ifs_other + def __repr__(self): + return '' % (self.ifs_whitespace, + self.ifs_other) + def Split(self, s, allow_escape): # type: (str, bool) -> List[Span] """ diff --git a/osh/split_test.py b/osh/split_test.py index 621ba640fc..4aa7f6d1d7 100755 --- a/osh/split_test.py +++ b/osh/split_test.py @@ -5,6 +5,8 @@ import unittest +from core import state +from core import test_lib from osh import split # module under test @@ -160,5 +162,21 @@ def testTwoOther(self): _RunSplitCases(self, sp, CASES) +class SplitContextTest(unittest.TestCase): + + def testSplitForWordEval(self): + arena = test_lib.MakeArena('') + mem = state.Mem('', [], arena, [], {}) + # This is the default + #state.SetGlobalString(mem, 'IFS', split.DEFAULT_IFS) + + splitter = split.SplitContext(mem) + + # Can pass allow_escape=False + for s in ['', ' foo bar ', '\\']: + result = splitter.SplitForWordEval(s) + print(result) + + if __name__ == '__main__': unittest.main() From f34b96650e49d82aae741d6a59d10e3eba5b5222 Mon Sep 17 00:00:00 2001 From: Andy C Date: Wed, 27 Nov 2024 11:19:05 -0500 Subject: [PATCH 2/2] [types] Fix build --- osh/split.py | 1 + 1 file changed, 1 insertion(+) diff --git a/osh/split.py b/osh/split.py index 912e7f820a..9296c820cf 100644 --- a/osh/split.py +++ b/osh/split.py @@ -226,6 +226,7 @@ def __init__(self, ifs_whitespace, ifs_other): self.ifs_other = ifs_other def __repr__(self): + # type: () -> str return '' % (self.ifs_whitespace, self.ifs_other)