Skip to content

file cannot be used as subject for some modifiers #331

Open

Description

@LukeSavefrogs

Problem

Reading the documentation it is said that file is a subject, and later it is said that modifiers can be applied to subjects (no reported limit here):
image

Example

Given these info I would have expected the following to work:

# shellcheck shell=bash

setup () {
    local temp_dir="$1"
    %logger "INFO - Creating temporary directory: $temp_dir"
    mkdir -p "$temp_dir"
}
cleanup () {
    local temp_dir="$1"
    %logger "INFO - Removing temporary directory: $temp_dir"
    rm -rf "$temp_dir"
}

dummy_process () {
    printf "id,column\n" > "$1/output.csv"
    printf "%s,data\n" {1..20} >> "$1/output.csv"
}

ExampleGroup "[E2E] Script 'interface_report'" needs:server-on
    temp_dir="$(mktemp --directory)"

    BeforeEach "setup '$temp_dir'"
    AfterEach "cleanup '$temp_dir'"

    It 'outputs the csv'
        When run dummy_process "$temp_dir"

        # ------ OK ------
        The status should be success
        The file "${temp_dir}/output.csv" should be exist
        The file "${temp_dir}/output.csv" should be readable
        The contents of file "${temp_dir}/output.csv" should include "1,data"

        # ------ FAIL ------
        # However, the logger correctly shows `Lines of output file: 21`
        %logger "Lines of output file: $(wc -l < "${temp_dir}/output.csv")"

        The lines of file "${temp_dir}/output.csv" should equal 21
        The line 1 of file "${temp_dir}/output.csv" should equal "id,column"
    End
End

You can try for yourself by saving it to a spec file.

Output:

Running: /usr/bin/bash [bash 5.1.4(1)-release]

INFO - Creating temporary directory: /tmp/tmp.vblrw8ETZh
Lines of output file: 21
INFO - Removing temporary directory: /tmp/tmp.vblrw8ETZh
[E2E] Script 'interface_report'
  outputs the csv (FAILED - 1)

Examples:
  1) [E2E] Script 'interface_report' outputs the csv
     When run test_data /tmp/tmp.vblrw8ETZh

     1.1) The line 1 of file /tmp/tmp.vblrw8ETZh/output.csv should equal id,column

            expected: "id,column"
                 got: "/tmp/tmp.vblrw8ETZh/output.csv"

          # spec/e2e/interface_spec.sh:37

     1.2) The lines of file /tmp/tmp.vblrw8ETZh/output.csv should equal 21

            expected: 21
                 got: 1

          # spec/e2e/interface_spec.sh:38

Finished in 0.12 seconds (user 0.09 seconds, sys 0.02 seconds)
1 example, 1 failure

Is this something that can be implemented? I think it would be very useful while testing complex scripts that write logs or output files

Activity

  1. LukeSavefrogs commented on Aug 21, 2024

    @LukeSavefrogs
    ContributorAuthor

    Workaround

    It seems like file while used together with lines, line or other unsupported modifiers is treated as if value was used:

    Examples:
      1) [E2E] Script 'interface_report' outputs the csv
         When run dummy_process /tmp/tmp.m3P638uY1U
    
         1.1) The lines of file /tmp/tmp.m3P638uY1U/output.csv should equal 21
    
                expected: 21
                     got: 1
    
              # spec/e2e/file_spec.sh:41
    
         1.2) The lines of value /tmp/tmp.m3P638uY1U/output.csv should equal 21
    
                expected: 21
                     got: 1
    
              # spec/e2e/file_spec.sh:42
    
         1.3) The line 1 of file /tmp/tmp.m3P638uY1U/output.csv should equal id,column
    
                expected: "id,column"
                     got: "/tmp/tmp.m3P638uY1U/output.csv"
    
              # spec/e2e/file_spec.sh:44
    
         1.4) The line 1 of value /tmp/tmp.m3P638uY1U/output.csv should equal id,column
    
                expected: "id,column"
                     got: "/tmp/tmp.m3P638uY1U/output.csv"
    
              # spec/e2e/file_spec.sh:45
    
    Finished in 0.14 seconds (user 0.13 seconds, sys 0.01 seconds)
    1 example, 1 failure
    

    This lead to the following workaround, which uses value together with command substitution to perform the right check:

    # shellcheck shell=bash
    
    setup () {
        local temp_dir="$1"
        %logger "INFO - Creating temporary directory: $temp_dir"
        mkdir -p "$temp_dir"
    }
    cleanup () {
        local temp_dir="$1"
        %logger "INFO - Removing temporary directory: $temp_dir"
        rm -rf "$temp_dir"
    }
    
    dummy_process () {
        printf "id,column\n" > "$1/output.csv"
        printf "%s,data\n" {1..20} >> "$1/output.csv"
    }
    
    ExampleGroup "[E2E] Script 'interface_report'" needs:server-on
        temp_dir="$(mktemp --directory)"
    
        BeforeEach "setup '$temp_dir'"
        AfterEach "cleanup '$temp_dir'"
    
        It 'outputs the csv'
            When run dummy_process "$temp_dir"
            The status should be success
    
    
            # ------ OK ------
            The file "${temp_dir}/output.csv" should be exist
            The file "${temp_dir}/output.csv" should be readable
            The contents of file "${temp_dir}/output.csv" should include "1,data"
    
            # ------ FIXED ------
            # Logger correctly shows `Lines of output file: 21`
            %logger "Lines of output file: $(wc -l < "${temp_dir}/output.csv")"
    
            The lines of value "$(cat "${temp_dir}/output.csv")" should equal 21
            The line 1 of value "$(cat "${temp_dir}/output.csv")" should equal "id,column"
        End
    End

    Output:

    Running: /usr/bin/bash [bash 5.1.4(1)-release]
    
    INFO - Creating temporary directory: /tmp/tmp.opTM54pszp
    Lines of output file: 21
    INFO - Removing temporary directory: /tmp/tmp.opTM54pszp
    [E2E] Script 'interface_report'
      outputs the csv
    
    Finished in 0.12 seconds (user 0.10 seconds, sys 0.01 seconds)
    1 example, 0 failures
    
  2. LukeSavefrogs commented on Aug 21, 2024

    @LukeSavefrogs
    ContributorAuthor

    I think however that there should be an "official" way to do these kind of checks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions