Navigation Menu

Skip to content

Commit

Permalink
[fix] Fix unit tests by initializing mem.exec_opts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Nov 4, 2020
1 parent 55eec23 commit 1517e6b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
12 changes: 11 additions & 1 deletion core/completion_test.py
Expand Up @@ -53,8 +53,11 @@ def _MakeRootCompleter(parse_ctx=None, comp_lookup=None):
comp_lookup = comp_lookup or completion.Lookup()

mem = state.Mem('', [], None, [])
state.InitMem(mem, {}, '0.1')
parse_opts, exec_opts, mutable_opts = state.MakeOpts(mem, None)
mem.exec_opts = exec_opts

state.InitMem(mem, {}, '0.1')
mutable_opts.Init()

if not parse_ctx:
parse_ctx = test_lib.InitParseContext(parse_opts=parse_opts)
Expand Down Expand Up @@ -122,6 +125,9 @@ def testLookup(self):

def testExternalCommandAction(self):
mem = state.Mem('dummy', [], None, [])
parse_opts, exec_opts, mutable_opts = state.MakeOpts(mem, None)
mem.exec_opts = exec_opts

a = completion.ExternalCommandAction(mem)
comp = self._CompApi([], 0, 'f')
print(list(a.Matches(comp)))
Expand Down Expand Up @@ -731,6 +737,10 @@ def testMatchesOracle(self):
arena = test_lib.MakeArena('<InitCompletionTest>')
parse_ctx = test_lib.InitParseContext(arena=arena)
mem = state.Mem('', [], arena, [])
parse_opts, exec_opts, mutable_opts = state.MakeOpts(mem, None)
mem.exec_opts = exec_opts

mutable_opts.Init()

#
# Allow our code to access oracle data
Expand Down
7 changes: 3 additions & 4 deletions core/process_test.py
Expand Up @@ -35,12 +35,11 @@ def Banner(msg):
_ARENA = test_lib.MakeArena('process_test.py')

_MEM = state.Mem('', [], _ARENA, [])
_PARSE_OPTS, _EXEC_OPTS, _MUTABLE_OPTS = state.MakeOpts(_MEM, None)
_MEM.exec_opts = _EXEC_OPTS

state.InitMem(_MEM, {}, '0.1')

_OPT0_ARRAY = [False] * option_i.ARRAY_SIZE
_OPT_STACKS = [False] * option_i.ARRAY_SIZE
_PARSE_OPTS = optview.Parse(_OPT0_ARRAY, _OPT_STACKS)
_EXEC_OPTS = state.MutableOpts(_MEM, _OPT0_ARRAY, _OPT_STACKS, None)
_JOB_STATE = process.JobState()
_WAITER = process.Waiter(_JOB_STATE, _EXEC_OPTS)
_ERRFMT = ui.ErrorFormatter(_ARENA)
Expand Down
11 changes: 6 additions & 5 deletions core/pure.py
Expand Up @@ -254,6 +254,12 @@ def Main(lang, arg_r, environ, login_shell, loader, line_input):
arg_r.Next()
mem = state.Mem(dollar0, arg_r.Rest(), arena, debug_stack)

opt_hook = state.OptHook()
parse_opts, exec_opts, mutable_opts = state.MakeOpts(mem, opt_hook)
# Note: only MutableOpts needs mem, so it's not a true circular dep.
mem.exec_opts = exec_opts # circular dep
mutable_opts.Init()

version_str = pyutil.GetVersion(loader)
state.InitMem(mem, environ, version_str)

Expand All @@ -262,11 +268,6 @@ def Main(lang, arg_r, environ, login_shell, loader, line_input):
job_state = process.JobState()
fd_state = process.FdState(errfmt, job_state, mem)

opt_hook = state.OptHook()
parse_opts, exec_opts, mutable_opts = state.MakeOpts(mem, opt_hook)
# TODO: only MutableOpts needs mem, so it's not a true circular dep.
mem.exec_opts = exec_opts # circular dep
mutable_opts.Init()

if attrs.show_options: # special case: sh -o
mutable_opts.ShowOptions([])
Expand Down
2 changes: 1 addition & 1 deletion core/shell.py
Expand Up @@ -273,7 +273,7 @@ def Main(lang, arg_r, environ, login_shell, loader, line_input):
mem = state.Mem(dollar0, arg_r.Rest(), arena, debug_stack)

opt_hook = ShellOptHook(line_input)
# TODO: only MutableOpts needs mem, so it's not a true circular dep.
# Note: only MutableOpts needs mem, so it's not a true circular dep.
parse_opts, exec_opts, mutable_opts = state.MakeOpts(mem, opt_hook)
mem.exec_opts = exec_opts # circular dep
mutable_opts.Init()
Expand Down
19 changes: 14 additions & 5 deletions core/test_lib.py
Expand Up @@ -131,11 +131,12 @@ def InitLexer(s, arena):
def InitWordEvaluator(exec_opts=None):
arena = MakeArena('<InitWordEvaluator>')
mem = state.Mem('', [], arena, [])
state.InitMem(mem, {}, '0.1')

if exec_opts is None:
parse_opts, exec_opts, mutable_opts = state.MakeOpts(mem, None)
mem.exec_opts = exec_opts # circular dep
state.InitMem(mem, {}, '0.1')
mutable_opts.Init()

cmd_deps = cmd_eval.Deps()
cmd_deps.trap_nodes = []
Expand All @@ -158,9 +159,12 @@ def InitCommandEvaluator(parse_ctx=None, comp_lookup=None, arena=None, mem=None,
parse_ctx = InitParseContext()

mem = mem or state.Mem('', [], arena, [])
state.InitMem(mem, {}, '0.1')
exec_opts = optview.Exec(opt0_array, opt_stacks)
mutable_opts = state.MutableOpts(mem, opt0_array, opt_stacks, None)
mem.exec_opts = exec_opts
state.InitMem(mem, {}, '0.1')
mutable_opts.Init()

# No 'readline' in the tests.

errfmt = ui.ErrorFormatter(arena)
Expand Down Expand Up @@ -244,19 +248,24 @@ def InitCommandEvaluator(parse_ctx=None, comp_lookup=None, arena=None, mem=None,

def EvalCode(code_str, parse_ctx, comp_lookup=None, mem=None, aliases=None):
"""
Unit tests can evaluate code strings and then use the resulting CommandEvaluator.
Unit tests can evaluate code strings and then use the resulting
CommandEvaluator.
"""
arena = parse_ctx.arena

comp_lookup = comp_lookup or completion.Lookup()
mem = mem or state.Mem('', [], arena, [])
parse_opts, exec_opts, mutable_opts = state.MakeOpts(mem, None)
mem.exec_opts = exec_opts

state.InitMem(mem, {}, '0.1')
mutable_opts.Init()

line_reader, _ = InitLexer(code_str, arena)
c_parser = parse_ctx.MakeOshParser(line_reader)

cmd_ev = InitCommandEvaluator(parse_ctx=parse_ctx, comp_lookup=comp_lookup, arena=arena,
mem=mem, aliases=aliases)
cmd_ev = InitCommandEvaluator(parse_ctx=parse_ctx, comp_lookup=comp_lookup,
arena=arena, mem=mem, aliases=aliases)

main_loop.Batch(cmd_ev, c_parser, arena) # Parse and execute!
return cmd_ev
Expand Down
3 changes: 2 additions & 1 deletion osh/arith_parse_test.py
Expand Up @@ -35,8 +35,9 @@ def ParseAndEval(code_str):
print('node:', anode)

mem = state.Mem('', [], arena, [])
state.InitMem(mem, {}, '0.1')
parse_opts, exec_opts, mutable_opts = state.MakeOpts(mem, None)
mem.exec_opts = exec_opts
state.InitMem(mem, {}, '0.1')

splitter = split.SplitContext(mem)
errfmt = ui.ErrorFormatter(arena)
Expand Down

0 comments on commit 1517e6b

Please sign in to comment.