Skip to content
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

BeforeCall and AfterCall hook problem #255

Open
shinokada opened this issue Jan 3, 2022 · 1 comment
Open

BeforeCall and AfterCall hook problem #255

shinokada opened this issue Jan 3, 2022 · 1 comment

Comments

@shinokada
Copy link

shinokada commented Jan 3, 2022

I'd like to test a function that counts the number of files in a directory.

By using BeforeCall and AfterCall hooks, I create a temporary directory and files before the call, and delete them after the call.

But I get an error. What am I doing wrong here?

Describe 'The count function'
        setup(){ 
            mkdir -p /tmp/count_tmp
            touch /tmp/count_tmp/file1.txt /tmp/count_tmp/file2.txt
            }
        cleanup(){ rm -rf /tmp/count_tmp;}
        BeforeCall 'setup'
        AfterCall 'cleanup'
        
        It 'counts files or directories in directory'
            When call count /tmp/count_tmp/*
            The output should eq 2
        End
End

Error:

3.1) The output should eq 2

            expected: 2
                 got: 0

count function:

count() {
    # Count files or directories in directory
    # Usage: count /path/to/dir/*
    #        count /path/to/dir/*/
    #  count ~/Downloads/*
    # 232

    [ -e "$1" ] &&
        printf '%s\n' "$#" ||
        printf '%s\n' 0
}

Are there better ways to test them?

@shinokada shinokada changed the title What is the best way to create temporary files in a temporary directory BeforeCall and AfterCall hook problem Jan 4, 2022
@lutzmad
Copy link

lutzmad commented Feb 5, 2022

Hello Shinichi Okada,
nice to see the issue, I was astonished in past because some of my tests do not passed and I could not find out what is going wrong.
It seems to me the path to the folder is substituted in the test section before the BeforeCall was called. If the folder is already available, the test will passed.

This is not a fix of your problem, but you confirm to me, it is a problem of the test tool.

With regards,
Lutz

Appendage:
I create the folder in a additional dummy test first, this is a work around only.

Describe 'The count function'

        setup(){
            mkdir -p /tmp/count_tmp
            touch /tmp/count_tmp/file1.txt /tmp/count_tmp/file2.txt
            }

        cleanup(){
            rm -rf /tmp/count_tmp;}

    Describe 'Dummy function test'
        BeforeRun 'setup'

        It 'create the directory only'
            When run true
            The status should eq 0
        End
    End

    Describe 'The count function test'
#        BeforeCall 'setup'
        AfterCall 'cleanup'
        Include count.sh

        It 'counts files or directories in directory'
            When call count /tmp/count_tmp/*
            The output should eq 2
        End
    End
End

The test will pass, now.

Running: /bin/sh [bash 3.2.57(1)-release]
..

Finished in 0.31 seconds (user 0.33 seconds, sys 0.04 seconds)
2 examples, 0 failures

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants