- 
                Notifications
    You must be signed in to change notification settings 
- Fork 158
Capture the environment variables in TimerAction #728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            sjdalessandro
  wants to merge
  3
  commits into
  ros2:rolling
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
sjdalessandro:dalessandro/capture_envs_in_timer_action
  
      
      
   
  
    
  
  
  
 
  
      
    base: rolling
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +445
        
        
          −31
        
        
          
        
      
    
  
  
     Open
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            3 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # Copyright 2022 Open Source Robotics Foundation, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|  | ||
| """Module for the ReplaceEnvironmentVariables action.""" | ||
|  | ||
| from typing import Mapping | ||
| from typing import Text | ||
|  | ||
| from ..action import Action | ||
| from ..launch_context import LaunchContext | ||
|  | ||
|  | ||
| class ReplaceEnvironmentVariables(Action): | ||
| """ | ||
| Action that replaces the environment variables in the current context. | ||
|  | ||
| The previous state can be saved by pushing the stack with the | ||
| :py:class:`launch.actions.PushEnvironment` action. | ||
| And can be restored by popping the stack with the | ||
| :py:class:`launch.actions.PopEnvironment` action. | ||
| """ | ||
|  | ||
| def __init__(self, environment: Mapping[Text, Text], **kwargs) -> None: | ||
| """Create a ReplaceEnvironmentVariables action.""" | ||
| super().__init__(**kwargs) | ||
| self.__environment = environment | ||
|  | ||
| def execute(self, context: LaunchContext): | ||
| """Execute the action.""" | ||
| context._replace_environment(self.__environment) | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            80 changes: 80 additions & 0 deletions
          
          80 
        
  launch/test/launch/actions/test_replace_environment_variables.py
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Copyright 2022 Open Source Robotics Foundation, Inc. | ||
|         
                  sjdalessandro marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|  | ||
| """Tests for the ReplaceEnvironmentVariables action classes.""" | ||
|  | ||
| import os | ||
|  | ||
| from launch import LaunchContext | ||
| from launch.actions import PopEnvironment | ||
| from launch.actions import PushEnvironment | ||
| from launch.actions import ReplaceEnvironmentVariables | ||
|  | ||
| from temporary_environment import sandbox_environment_variables | ||
|  | ||
|  | ||
| @sandbox_environment_variables | ||
| def test_replace_environment_constructors(): | ||
| """Test the constructors for ReplaceEnvironmentVariables class.""" | ||
| ReplaceEnvironmentVariables([]) | ||
| ReplaceEnvironmentVariables([('foo', 'bar'), ('spam', 'eggs')]) | ||
|  | ||
|  | ||
| @sandbox_environment_variables | ||
| def test_replace_environment_execute(): | ||
| """Test the execute() of the ReplaceEnvironmentVariables class.""" | ||
| assert isinstance(os.environ, os._Environ) | ||
|  | ||
| # replaces empty state | ||
| context = LaunchContext() | ||
| context.environment.clear() | ||
| assert len(context.environment) == 0 | ||
| ReplaceEnvironmentVariables([('foo', 'bar'), ('spam', 'eggs')]).visit(context) | ||
| assert len(context.environment) == 2 | ||
| assert 'foo' in context.environment | ||
| assert context.environment['foo'] == 'bar' | ||
| assert 'spam' in context.environment | ||
| assert context.environment['spam'] == 'eggs' | ||
|  | ||
| # replaces non empty state | ||
| context = LaunchContext() | ||
| context.environment.clear() | ||
| assert len(context.environment) == 0 | ||
| context.environment['quux'] = 'quuux' | ||
| assert len(context.environment) == 1 | ||
| assert 'quux' in context.environment | ||
| assert context.environment['quux'] == 'quuux' | ||
| ReplaceEnvironmentVariables([('foo', 'bar'), ('spam', 'eggs')]).visit(context) | ||
|         
                  sjdalessandro marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| assert len(context.environment) == 2 | ||
| assert 'foo' in context.environment | ||
| assert context.environment['foo'] == 'bar' | ||
| assert 'spam' in context.environment | ||
| assert context.environment['spam'] == 'eggs' | ||
|  | ||
| # Replacing the environment should not change the type of os.environ | ||
| assert isinstance(os.environ, os._Environ) | ||
|  | ||
| # does not interfere with PopEnvironment and PushEnvironment action classes | ||
| context = LaunchContext() | ||
| context.environment.clear() | ||
| context.environment['quux'] = 'quuux' | ||
| assert len(context.environment) == 1 | ||
| assert 'quux' in context.environment | ||
| assert context.environment['quux'] == 'quuux' | ||
| PushEnvironment().visit(context) | ||
| ReplaceEnvironmentVariables([('foo', 'bar'), ('spam', 'eggs')]).visit(context) | ||
| PopEnvironment().visit(context) | ||
| assert len(context.environment) == 1 | ||
| assert 'quux' in context.environment | ||
| assert context.environment['quux'] == 'quuux' | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.