def test_set_value_that_references_an_env_var(self): # ARRANGE # existing_env_var = NameAndValue('MY_VAR', 'MY_VAL') defined_env_var = NameAndValue('name', existing_env_var.value) environ__before = { existing_env_var.name: existing_env_var.value, } environ__after = { defined_env_var.name: defined_env_var.value, existing_env_var.name: existing_env_var.value, } # ACT & ASSERT # CHECKER.check__abs_stx__std_layouts_and_source_variants( self, SetVariableArgumentsAbsStx.of_str(defined_env_var.name, env_var_ref_syntax( existing_env_var.name), phase_spec=None), arrangement=Arrangement.setup_phase_aware( process_execution_settings=ProcessExecutionSettings. with_environ(environ__before), ), expectation=MultiSourceExpectation.setup_phase_aware( main_side_effect_on_environment_variables=asrt.equals( environ__after), ))
def test_a_references_to_a_non_existing_env_var_SHOULD_be_replaced_with_empty_string( self): # ARRANGE # existing_var = NameAndValue('existing', 'EXISTING') non_existing_var__name = 'non_existing' new_var_to_set__name = 'new_var_to_set' source = SetVariableArgumentsAbsStx.of_str( new_var_to_set__name, env_var_ref_syntax(non_existing_var__name), phase_spec=None, ) # ACT & ASSERT # CHECKER.check__abs_stx__std_layouts_and_source_variants( self, source, Arrangement.setup_phase_aware( process_execution_settings=proc_exe_env_for_test( environ={existing_var.name: existing_var.value})), MultiSourceExpectation.setup_phase_aware( main_result=asrt.is_none, main_side_effect_on_environment_variables=asrt.equals({ existing_var.name: existing_var.value, new_var_to_set__name: '', })))
def test_constant_string_value__current_is_not_none(self): # ACT & ASSERT # var = NameAndValue('name', 'value') CHECKER.check__abs_stx__std_layouts_and_source_variants( self, SetVariableArgumentsAbsStx.of_nav(var, phase_spec=None), Arrangement.setup_phase_aware( process_execution_settings=proc_exe_env_for_test(environ={})), MultiSourceExpectation.setup_phase_aware( main_result=asrt.is_none, main_side_effect_on_environment_variables=asrt.equals( {var.name: var.value})))
def test_main_method_arguments(self): # ARRANGE # the_environ = MappingProxyType({'an_env_var': 'an env var value'}) the_timeout = 72 the_os_services = os_services_access.new_for_current_os() setup_settings_cases = [ NArrEx( 'none', None, asrt.is_none, ), NArrEx( 'not none', SetupSettingsArr(the_environ), _IsSettingsBuilderWoStdinWEnviron(asrt.equals(the_environ)), ), ] for setup_settings_case in setup_settings_cases: def main_action_that_checks_arguments(environment: InstructionEnvironmentForPostSdsStep, instruction_settings: InstructionSettings, settings_builder: Optional[SetupSettingsBuilder], os_services: OsServices): self.assertIs(os_services, the_os_services, 'os_services') self.assertEqual(the_environ, environment.proc_exe_settings.environ, 'proc exe settings/environment') self.assertEqual(the_timeout, environment.proc_exe_settings.timeout_in_seconds, 'proc exe settings/timeout') self.assertEqual(the_environ, instruction_settings.environ(), 'instruction settings/environment') setup_settings_case.expectation.apply_with_message(self, settings_builder, 'setup settings passed to main') # ACT & ASSERT # self._check_source_and_exe_variants( ParserThatGives(instruction_embryo_that__setup_phase_aware( main_initial_action=main_action_that_checks_arguments )), Arrangement.setup_phase_aware( process_execution_settings=ProcessExecutionSettings(the_timeout, the_environ), setup_settings=setup_settings_case.arrangement, os_services=the_os_services, ), MultiSourceExpectation.setup_phase_aware( setup_settings=asrt.anything_goes(), ), )
def test_value_SHOULD_be_able_to_have_symbol_references(self): # ARRANGE # variable_name = 'variable_to_assign' my_symbol = StringConstantSymbolContext('my_symbol', 'my symbol value') your_symbol = StringConstantSymbolContext('your_symbol', 'your symbol value') value_template = 'pre {MY_SYMBOL} {YOUR_SYMBOL} post' expected_evaluated_value_string = value_template.format( MY_SYMBOL=my_symbol.str_value, YOUR_SYMBOL=your_symbol.str_value, ) expected_environ_after_main = { variable_name: expected_evaluated_value_string, } value_source_string = value_template.format( MY_SYMBOL=symbol_reference_syntax_for_name(my_symbol.name), YOUR_SYMBOL=symbol_reference_syntax_for_name(your_symbol.name), ) source_syntax = SetVariableArgumentsAbsStx.of_str( variable_name, value_source_string, phase_spec=None, quoting=QuoteType.SOFT) # ACT & ASSERT # CHECKER.check__abs_stx__std_layouts_and_source_variants( self, source_syntax, Arrangement.setup_phase_aware( symbols=SymbolContext.symbol_table_of_contexts( [my_symbol, your_symbol]), process_execution_settings=ProcessExecutionSettings. with_environ({}), ), MultiSourceExpectation.setup_phase_aware( main_side_effect_on_environment_variables=asrt.equals( expected_environ_after_main), symbol_usages=asrt.matches_sequence([ my_symbol.usage_assertion__w_str_rendering, your_symbol.usage_assertion__w_str_rendering, ]), ), )
def test_existing_variable(self): var_a = NameAndValue('a', 'A') var_b = NameAndValue('b', 'B') environ__before = NameAndValue.as_dict([var_a, var_b]) environ__after = NameAndValue.as_dict([var_b]) CHECKER.check__abs_stx__std_layouts_and_source_variants( self, UnsetVariableArgumentsAbsStx.of_str(var_a.name, phase_spec=None), Arrangement.setup_phase_aware( process_execution_settings=ProcessExecutionSettings. with_environ(environ__before), ), MultiSourceExpectation.setup_phase_aware( main_side_effect_on_environment_variables=asrt.equals( environ__after), ))
def test_set_value_that_contains_text_and_references_to_env_vars(self): # ARRANGE # my_var = NameAndValue('MY_VAR', 'MY_VAL') your_var = NameAndValue('YOUR_VAR', 'YOUR_VAL') var_to_set_name = 'name' value_template = 'pre {my_var} {your_var} post' source = SetVariableArgumentsAbsStx.of_str( var_to_set_name, value_template.format( my_var=env_var_ref_syntax(my_var.name), your_var=env_var_ref_syntax(your_var.name), ), phase_spec=None, quoting=QuoteType.SOFT, ) expected_value = value_template.format( my_var=my_var.value, your_var=your_var.value, ) environ__before = { my_var.name: my_var.value, your_var.name: your_var.value, } expected_environ__after = { var_to_set_name: expected_value, my_var.name: my_var.value, your_var.name: your_var.value, } # ACT & ASSERT # CHECKER.check__abs_stx__std_layouts_and_source_variants( self, source, arrangement=Arrangement.setup_phase_aware( process_execution_settings=ProcessExecutionSettings. with_environ(environ__before), ), expectation=MultiSourceExpectation.setup_phase_aware( main_side_effect_on_environment_variables=asrt.equals( expected_environ__after), ))
def test_WHEN_var_name_is_not_an_existing_env_var_THEN_env_SHOULD_be_unmodified( self): non_existing_var_name = 'non_existing' existing_var = NameAndValue('existing', 'EXISTING') environ__before = NameAndValue.as_dict([existing_var]) environ__after = NameAndValue.as_dict([existing_var]) CHECKER.check__abs_stx__std_layouts_and_source_variants( self, UnsetVariableArgumentsAbsStx.of_str(non_existing_var_name, phase_spec=None), Arrangement.setup_phase_aware( process_execution_settings=ProcessExecutionSettings. with_environ(environ__before), ), MultiSourceExpectation.setup_phase_aware( main_side_effect_on_environment_variables=asrt.equals( environ__after), ))
def test_current_environ_is_none(self): var_a = NameAndValue('a', 'A') var_b = NameAndValue('b', 'B') environ__before = NameAndValue.as_dict([var_a, var_b]) environ__after = NameAndValue.as_dict([var_b]) def get_default_environ() -> Dict[str, str]: return dict(environ__before) CHECKER.check__abs_stx__std_layouts_and_source_variants( self, UnsetVariableArgumentsAbsStx.of_str(var_a.name, phase_spec=None), Arrangement.setup_phase_aware( process_execution_settings=proc_exe_env_for_test(environ=None), default_environ_getter=get_default_environ, ), MultiSourceExpectation.setup_phase_aware( main_side_effect_on_environment_variables=asrt.equals( environ__after), ))
def test_multi_line_value(self): # ARRANGE # var = NameAndValue('name', 'a\nmulti\nline\nvalue\n') environ__before = {} environ__after = {var.name: var.value} # ACT & ASSERT # CHECKER.check__abs_stx__std_layouts_and_source_variants( self, SetVariableArgumentsAbsStx.of_str(var.name, var.value, phase_spec=None, quoting=QuoteType.HARD), Arrangement.setup_phase_aware( process_execution_settings=proc_exe_env_for_test( environ=environ__before)), MultiSourceExpectation.setup_phase_aware( main_result=asrt.is_none, main_side_effect_on_environment_variables=asrt.equals( environ__after)))
def test_current_environ_is_none(self): # ACT & ASSERT # var_in_default = NameAndValue('var_in_default', 'value of var in default') def get_default_environ() -> Dict[str, str]: return NameAndValue.as_dict([var_in_default]) var_to_set = NameAndValue('var_to_set', 'value') CHECKER.check__abs_stx__std_layouts_and_source_variants( self, SetVariableArgumentsAbsStx.of_nav(var_to_set, phase_spec=None), Arrangement.setup_phase_aware( process_execution_settings=proc_exe_env_for_test(environ=None), default_environ_getter=get_default_environ, ), MultiSourceExpectation.setup_phase_aware( main_result=asrt.is_none, main_side_effect_on_environment_variables=asrt.equals( NameAndValue.as_dict([var_in_default, var_to_set]))))
def test_value_with_hard_quoted_value_SHOULD_skip_symbol_substitution( self): # ARRANGE # variable_name = 'variable_to_assign' my_symbol = NameAndValue('my_symbol', 'my symbol value') your_symbol = NameAndValue('your_symbol', 'your symbol value') value_template = 'pre {MY_SYMBOL} {YOUR_SYMBOL} post' value_source_string = value_template.format( MY_SYMBOL=symbol_reference_syntax_for_name(my_symbol.name), YOUR_SYMBOL=symbol_reference_syntax_for_name(your_symbol.name), ) expected_environ_after_main = { variable_name: value_source_string, } source_syntax = SetVariableArgumentsAbsStx.of_str( variable_name, value_source_string, phase_spec=None, quoting=QuoteType.HARD) # ACT & ASSERT # CHECKER.check__abs_stx__std_layouts_and_source_variants( self, source_syntax, Arrangement.setup_phase_aware( process_execution_settings=ProcessExecutionSettings. with_environ({}), ), MultiSourceExpectation.setup_phase_aware( main_side_effect_on_environment_variables=asrt.equals( expected_environ_after_main), symbol_usages=asrt.matches_sequence([]), ), )
def test_WHEN_env_contains_the_var_being_set_THEN_its_value_SHOULD_be_replaced( self): # ACT & ASSERT # var_name = 'ENV_VAR' value_before = 'before' value_after = 'after' environ__before = {var_name: value_before} environ__after = {var_name: value_after} CHECKER.check__abs_stx__std_layouts_and_source_variants( self, SetVariableArgumentsAbsStx.of_str(var_name, value_after, phase_spec=None), Arrangement.setup_phase_aware( process_execution_settings=proc_exe_env_for_test( environ=environ__before)), MultiSourceExpectation.setup_phase_aware( main_result=asrt.is_none, main_side_effect_on_environment_variables=asrt.equals( environ__after)))
def test_set_value_that_references_an_env_var_in_string_ref_value(self): # ARRANGE # existing_env_var = NameAndValue('MY_VAR', 'MY_VAL') defined_env_var = NameAndValue('name', existing_env_var.value) source_string_symbol_w_env_var_ref = StringSourceSymbolContext.of_primitive_constant( 'SYMBOL_W_ENV_VAR_REF', env_var_ref_syntax(existing_env_var.name)) environ__before = { existing_env_var.name: existing_env_var.value, } environ__after = { defined_env_var.name: defined_env_var.value, existing_env_var.name: existing_env_var.value, } # ACT & ASSERT # CHECKER.check__abs_stx__std_layouts_and_source_variants( self, SetVariableArgumentsAbsStx( StringLiteralAbsStx(defined_env_var.name), source_string_symbol_w_env_var_ref.abstract_syntax, phase_spec=None), arrangement=Arrangement.setup_phase_aware( symbols=source_string_symbol_w_env_var_ref.symbol_table, process_execution_settings=ProcessExecutionSettings. with_environ(environ__before), ), expectation=MultiSourceExpectation.setup_phase_aware( symbol_usages=source_string_symbol_w_env_var_ref. references_assertion, main_side_effect_on_environment_variables=asrt.equals( environ__after), ))
def test_assertion_on_setup_settings(self): # ARRANGE # environ__actual = {'actual_var': 'actual value'} environ__expected = {} the_setup_settings = SetupSettingsArr(environ=environ__actual) setup_settings_cases = [ NArrEx( 'none', None, asrt.is_not_none, ), NArrEx( 'not none', the_setup_settings, asrt_settings_builder.matches( environ=asrt.equals(environ__expected), stdin=asrt.is_none, ), ), ] for setup_settings_case in setup_settings_cases: # ACT & ASSERT # self._check_source_and_exe_variants__failing_assertions( ParserThatGives(instruction_embryo_that__setup_phase_aware()), Arrangement.setup_phase_aware( setup_settings=setup_settings_case.arrangement, ), MultiSourceExpectation.setup_phase_aware( setup_settings=setup_settings_case.expectation, ), sub_test_identifiers={ 'setup_settings': setup_settings_case.name, }, )
def runTest(self): # ACT & ASSERT # name = StringLiteralAbsStx('name') for phase_spec in PHASE_SPECS: for validation_case in str_src_validation_cases.failing_validation_cases( ): CHECKER.check__abs_stx__std_layouts_and_source_variants( self, SetVariableArgumentsAbsStx(name, validation_case.value.syntax, phase_spec=phase_spec), Arrangement.setup_phase_aware( symbols=validation_case.value.symbol_context. symbol_table), MultiSourceExpectation.setup_phase_aware( symbol_usages=validation_case.value.symbol_context. usages_assertion, validation=validation_case.value.assertion, ), sub_test_identifiers={ 'phase_spec': phase_spec, 'validation_case': validation_case.name, })
def test_fail_if_instruction_is_not_setup_phase_aware__source_variant(self): self._check_source_and_exe_variants__failing_assertions( ParserThatGives(instruction_embryo_that__phase_agnostic()), Arrangement.setup_phase_aware(), MultiSourceExpectation.setup_phase_aware(), )