Uploaded image for project: 'CFEngine Community'
  1. CFEngine Community
  2. CFE-3987

edit_line doesn't respect the region if a location is given with only the before_after attribute explicitly set to before

    XMLWordPrintable

    Details

      Description

      When trying to edit before a selected region I expected that text would be inserted immediately before the selected region, instead text is inserted at the very beginning of the document (which is indeed before the selected region, but not really near the selected region).

      Reproducer

      bundle agent __main__
      {
        methods:
            "init";
            "test";
            "check";
        reports:
            "/tmp/example.xml"
              printfile => my_cat($(this.promiser));
      }
      bundle edit_line ENT_8791
      {
        insert_lines:
            "INSERT ME"
              select_region => my_comment_last_filter,
              location => my_location("before"); # Insert before the selected region
      }
      body location my_location(before_after)
      # @brief Editing occurs before the matched line
      {
            before_after => "$(before_after)";
      }
      
      body select_region my_comment_last_filter
      {
              select_start => "\s+<!--.*";
              select_end => "\s+THIS MUST BE THE LAST FILTER IN THE DEFINED CHAIN";
              include_start_delimiter => "true";
              select_end_match_eof => "false";
      }
      
      bundle agent test
      {
        files:
            "/tmp/example.xml"
              edit_line => ENT_8791;
      }
      
      bundle agent check
      {
        classes:
            "found_expected_pattern"
            expression => regcmp( ".*INSERT\sME\R\s+<!--\s=+\R\s+THIS MUST BE THE LAST FILTER IN THE DEFINED CHAIN.*",
                                  readfile( "/tmp/example.xml" ) );
       reports:
          found_expected_pattern::
            "Pass $(this.promise_filename)";
          !found_expected_pattern::
            "FAIL $(this.promise_filename)";
      
      }
      bundle agent init
      {
        files:
            "/tmp/example.xml"
              content => `<?xml version="1.0"?>
            <web-app xmlns="http://java.sun.com/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
            version="3.0"
            metadata-complete="true">
            <!-- General -->
            <display-name>Atlassian JIRA Web Application</display-name>
            <description>The Atlassian JIRA web application - see http://www.atlassian.com/software/jira for more information
            </description>
      
            <absolute-ordering />
      
            <!-- Filters -->
      
            <!-- Special filters that must come at the beginning of the chain because they prevent
            all other filters from running.  This is to prevent those later filters from doing
            lookups in Pico, which could alter the order in which it instantiates components
            and thereby trigger a deadlock. -->
      
            <filter>
            <filter-name>JiraImportProgressFilter</filter-name>
            <filter-class&gt;com.atlassian.jira.web.filters.JiraImportProgressFilter</filter-class&gt;
            </filter>
      
            <!-- ========================================================
                 THIS MUST BE THE FIRST FILTER IN THE NORMAL FILTER CHAIN
                 ======================================================== -->
      
            <filter>
            <filter-name>JiraFirstFilter</filter-name>
            <filter-class&gt;com.atlassian.jira.web.filters.JiraFirstFilter</filter-class&gt;
            </filter>
      
            <!-- =====================================================
                 THIS MUST BE THE LAST FILTER IN THE DEFINED CHAIN
                 ===================================================== -->
      
            <filter>
            <filter-name>JiraLastFilter</filter-name>
            <filter-class&gt;com.atlassian.jira.web.filters.JiraLastFilter</filter-class&gt;
            </filter>
      
            <!-- =====================================================
                 FILTER MAPPINGS FOLLOW :
                 ===================================================== -->
      
      </web-app>`;
      
      }
      body printfile my_cat(file)
      # @brief Report the contents of a file
      # @param file The full path of the file to report
      {
              file_to_print => "$(file)";
              number_of_lines => "inf";
      }
      
      R: FAIL /home/nickanderson/org/roam/daily/work/cfengine3-TBLaRo
      R: /tmp/example.xml
      R: INSERT ME
      R: <?xml version="1.0"?>
      R:       <web-app xmlns="http://java.sun.com/xml/ns/javaee"
      R:       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      R:       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      R:       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      R:       version="3.0"
      R:       metadata-complete="true">
      R:       <!-- General -->
      R:       <display-name>Atlassian JIRA Web Application</display-name>
      R:       <description>The Atlassian JIRA web application - see http://www.atlassian.com/software/jira for more information
      R:       </description>
      R: 
      R:       <absolute-ordering />
      R: 
      R:       <!-- Filters -->
      R: 
      R:       <!-- Special filters that must come at the beginning of the chain because they prevent
      R:       all other filters from running.  This is to prevent those later filters from doing
      R:       lookups in Pico, which could alter the order in which it instantiates components
      R:       and thereby trigger a deadlock. -->
      R: 
      R:       <filter>
      R:       <filter-name>JiraImportProgressFilter</filter-name>
      R:       <filter-class>com.atlassian.jira.web.filters.JiraImportProgressFilter</filter-class>
      R:       </filter>
      R: 
      R:       <!-- ========================================================
      R:            THIS MUST BE THE FIRST FILTER IN THE NORMAL FILTER CHAIN
      R:            ======================================================== -->
      R: 
      R:       <filter>
      R:       <filter-name>JiraFirstFilter</filter-name>
      R:       <filter-class>com.atlassian.jira.web.filters.JiraFirstFilter</filter-class>
      R:       </filter>
      R: 
      R:       <!-- =====================================================
      R:            THIS MUST BE THE LAST FILTER IN THE DEFINED CHAIN
      R:            ===================================================== -->
      R: 
      R:       <filter>
      R:       <filter-name>JiraLastFilter</filter-name>
      R:       <filter-class>com.atlassian.jira.web.filters.JiraLastFilter</filter-class>
      R:       </filter>
      R: 
      R:       <!-- =====================================================
      R:            FILTER MAPPINGS FOLLOW :
      R:            ===================================================== -->
      R: 
      R: </web-app>
      

      Workaround

      I found that this can be worked around by explicitly specifying location.select_line_matching. When select_line_matchin When select_line_matchin When select_line_matchin When select_line_matchin When select_line_matchin When select_line_matchin When select_line_matchin When select_line_matching is explicitly specified, the text is inserted where I expect, just before the first matching line within the selected region.

      bundle agent __main__
      {
        methods:
            "init";
            "test";
            "check";
        reports:
            "/tmp/example.xml"
              printfile => my_cat($(this.promiser));
      }
      bundle edit_line ENT_8791
      {
        insert_lines:
            "INSERT ME"
              select_region => my_comment_last_filter,
              location => my_location("before"); # Insert before the selected region
      }
      body location my_location(before_after)
      # @brief Editing occurs before the matched line
      {
            before_after => "$(before_after)";
      
            # Workaround CFE-3987
            select_line_matching => ".*"; # With this explicitly specified, text is
                                          # inserted immediately before the first
                                          # matched line (note, location.first_last
                                          # defaults to first) and not at the
                                          # beginning of the file.
      }
      
      body select_region my_comment_last_filter
      {
              select_start => "\s+<!--.*";
              select_end => "\s+THIS MUST BE THE LAST FILTER IN THE DEFINED CHAIN";
              include_start_delimiter => "true";
              select_end_match_eof => "false";
      }
      
      bundle agent test
      {
        files:
            "/tmp/example.xml"
              edit_line => ENT_8791;
      }
      
      bundle agent check
      {
        classes:
            "found_expected_pattern"
            expression => regcmp( ".*INSERT\sME\R\s+<!--\s=+\R\s+THIS MUST BE THE LAST FILTER IN THE DEFINED CHAIN.*",
                                  readfile( "/tmp/example.xml" ) );
       reports:
          found_expected_pattern::
            "Pass $(this.promise_filename)";
          !found_expected_pattern::
            "FAIL $(this.promise_filename)";
      
      }
      bundle agent init
      {
        files:
            "/tmp/example.xml"
              content => `<?xml version="1.0"?>
            <web-app xmlns="http://java.sun.com/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
            version="3.0"
            metadata-complete="true">
            <!-- General -->
            <display-name>Atlassian JIRA Web Application</display-name>
            <description>The Atlassian JIRA web application - see http://www.atlassian.com/software/jira for more information
            </description>
      
            <absolute-ordering />
      
            <!-- Filters -->
      
            <!-- Special filters that must come at the beginning of the chain because they prevent
            all other filters from running.  This is to prevent those later filters from doing
            lookups in Pico, which could alter the order in which it instantiates components
            and thereby trigger a deadlock. -->
      
            <filter>
            <filter-name>JiraImportProgressFilter</filter-name>
            <filter-class&gt;com.atlassian.jira.web.filters.JiraImportProgressFilter</filter-class&gt;
            </filter>
      
            <!-- ========================================================
                 THIS MUST BE THE FIRST FILTER IN THE NORMAL FILTER CHAIN
                 ======================================================== -->
      
            <filter>
            <filter-name>JiraFirstFilter</filter-name>
            <filter-class&gt;com.atlassian.jira.web.filters.JiraFirstFilter</filter-class&gt;
            </filter>
      
            <!-- =====================================================
                 THIS MUST BE THE LAST FILTER IN THE DEFINED CHAIN
                 ===================================================== -->
      
            <filter>
            <filter-name>JiraLastFilter</filter-name>
            <filter-class&gt;com.atlassian.jira.web.filters.JiraLastFilter</filter-class&gt;
            </filter>
      
            <!-- =====================================================
                 FILTER MAPPINGS FOLLOW :
                 ===================================================== -->
      
      </web-app>`;
      
      }
      body printfile my_cat(file)
      # @brief Report the contents of a file
      # @param file The full path of the file to report
      {
              file_to_print => "$(file)";
              number_of_lines => "inf";
      }
      
      R: Pass /home/nickanderson/org/roam/daily/work/cfengine3-JpUGhj
      R: /tmp/example.xml
      R: <?xml version="1.0"?>
      R:       <web-app xmlns="http://java.sun.com/xml/ns/javaee"
      R:       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      R:       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      R:       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      R:       version="3.0"
      R:       metadata-complete="true">
      R:       <!-- General -->
      R:       <display-name>Atlassian JIRA Web Application</display-name>
      R:       <description>The Atlassian JIRA web application - see http://www.atlassian.com/software/jira for more information
      R:       </description>
      R: 
      R:       <absolute-ordering />
      R: 
      R:       <!-- Filters -->
      R: 
      R:       <!-- Special filters that must come at the beginning of the chain because they prevent
      R:       all other filters from running.  This is to prevent those later filters from doing
      R:       lookups in Pico, which could alter the order in which it instantiates components
      R:       and thereby trigger a deadlock. -->
      R: 
      R:       <filter>
      R:       <filter-name>JiraImportProgressFilter</filter-name>
      R:       <filter-class>com.atlassian.jira.web.filters.JiraImportProgressFilter</filter-class>
      R:       </filter>
      R: 
      R:       <!-- ========================================================
      R:            THIS MUST BE THE FIRST FILTER IN THE NORMAL FILTER CHAIN
      R:            ======================================================== -->
      R: 
      R:       <filter>
      R:       <filter-name>JiraFirstFilter</filter-name>
      R:       <filter-class>com.atlassian.jira.web.filters.JiraFirstFilter</filter-class>
      R:       </filter>
      R: 
      R: INSERT ME
      R:       <!-- =====================================================
      R:            THIS MUST BE THE LAST FILTER IN THE DEFINED CHAIN
      R:            ===================================================== -->
      R: 
      R:       <filter>
      R:       <filter-name>JiraLastFilter</filter-name>
      R:       <filter-class>com.atlassian.jira.web.filters.JiraLastFilter</filter-class>
      R:       </filter>
      R: 
      R:       <!-- =====================================================
      R:            FILTER MAPPINGS FOLLOW :
      R:            ===================================================== -->
      R: 
      R: </web-app>
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              a10042 Nick Anderson
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: