robotcodedev / robotcode

RobotFramework support for Visual Studio Code
https://robotcode.io
Apache License 2.0
184 stars 15 forks source link

[BUG] Global variable detection raised error message #321

Open jchabrol opened 1 month ago

jchabrol commented 1 month ago

Describe the bug A global variable create with a Set Test Variable wasn't detected and this variable wasn't see as defined

Steps To Reproduce

*** Keywords ***
Operator Triggers A Backup
    ${backup}       Post   ${URL}
    Set Test Variable       ${BACKUP_NAME_TEST_VAR}     ${backup}

The Backup Is Successfully Created
    [Arguments]     ${backup}=${BACKUP_NAME_TEST_VAR}
    ${backup_list}      Get   ${URL_LIST}
    Should Contain      ${backup_list}      backup/${backup[0]}

The error message is raised Variable '${BACKUP_NAME_TEST_VAR}' not found.robotcode.namespace(VariableNotFound)

Expected behavior The error message should not be raised as the variable is "globaly" defined

Screenshots/ Videos image image

Logs Variable '${BACKUP_NAME_TEST_VAR}' not found.robotcode.namespace(VariableNotFound)

Desktop (please complete the following information): Version: 1.93.1 Commit: 38c31bc77e0dd6ae88a4e9cc93428cc27a56ba40 Date: 2024-09-11T17:20:05.685Z Electron: 30.4.0 ElectronBuildId: 10073054 Chromium: 124.0.6367.243 Node.js: 20.15.1 V8: 12.4.254.20-electron.0 OS: Linux x64 6.8.0-45-generic snap

d-biehl commented 1 month ago

This is not a bug, it works as it should. This is because RobotCode performs a static code analysis and not a call analysis. It is not guaranteed that the keyword “Operator Triggers A Backup” will be called anywhere, anytime. And also not that exactly this variable ${BACKUP_NAME_TEST_VAR} is meant, maybe it is also set in some other test/keyword and it means something completely different.

What you should always do, also to find all references to this variable, is to define a default value, then you can change the value at runtime with Set Test/Suite/Global Variable, but you are always sure that this variable is really meant.

Something like this:

*** Variables ***
${BACKUP_NAME_TEST_VAR}    ${None}

*** Keywords ***
Operator Triggers A Backup
    ${backup}       Post   ${URL}
    Set Test Variable       ${BACKUP_NAME_TEST_VAR}     ${backup}

The Backup Is Successfully Created
    [Arguments]     ${backup}=${BACKUP_NAME_TEST_VAR}
    ${backup_list}      Get   ${URL_LIST}
    Should Contain      ${backup_list}      backup/${backup[0]}

With this method you can also validly check if the keyword Operator Triggers A Backup was executed correctly and the variable contains a valid value without getting an error like Resolving argument default values failed: Variable '${BACKUP_NAME_TEST_VAR}' not found., something like this:

*** Variables ***
${BACKUP_NAME_TEST_VAR}     ${None}

*** Keywords ***
Operator Triggers A Backup
    ${backup}       Post   ${URL}
    Set Test Variable       ${BACKUP_NAME_TEST_VAR}     ${backup}

The Backup Is Successfully Created
    [Arguments]    ${backup}=${BACKUP_NAME_TEST_VAR}
    IF    $backup is None
        Fail    No backup name provided
    ELSE
        Log    Backup name is ${backup}
    END
    ${backup_list}    Get    ${URL_LIST}
    Should Contain    ${backup_list}    backup/${backup[0]}

Hope this helps?