vitest-dev / vscode

VS Code extension for Vitest
https://vitest.dev/vscode
MIT License
729 stars 79 forks source link

Plugin needs an option to select a shell environment #403

Open Vlasterx opened 1 month ago

Vlasterx commented 1 month ago

Describe the bug

This is a bug that I have discovered to happen on Windows. Problem is in inability to select the working shell environment, like we have the option to select for the VSCode, and that all code probably executes in simple windows cmd terminal. This isn't working well for wrapper functions that I'm creating for NodeJS, and if wrapped code relies on that environment, tests fail.

More specifically, if I on Windows set Pageant and Cmder to use SSH keys, and then I access github from that environment, it will not be used by this plugin, but if I run Vitest command from Cmder, all tests will pass.

On Mac and Linux, this is not a problem. Tests run from the terminal on Windows that set to use SSH pass as well.

In summary - Plugin should reuse default shell/terminal environment already set for VSCode (terminal.integrated.profiles.windows) or if there are numerous already set, to have the possibility to select an env from the dropdown menu.

Reproduction

Since I cannot share an entire repository, I will describe the configuration and will provide an example code:

  1. Pageant Pageant should load the private key used to access a private repository

  2. Cmder Cmder should load Pageant ssh keys on start. That is done with setting the configuration in cmder\config\user_profile.cmd. Find this part and uncomment it to

    :: uncomment the next two lines to use pageant as the ssh authentication agent
    SET SSH_AUTH_SOCK=/tmp/.ssh-pageant-auth-sock
    call "%GIT_INSTALL_ROOT%/cmd/start-ssh-pageant.cmd"
  3. VSCode Set the default terminal to use Cmder in settings.json. My CMDER is installed in C:\utils\cmder\

    "terminal.integrated.profiles.windows": {
        "cmder": {
            "path": "C:\\WINDOWS\\System32\\cmd.exe",
            "args": [
                "/K",
                "C:\\utils\\cmder\\vendor\\bin\\vscode_init.cmd"
            ]
        },
    }

With this setup, VSCode will work with SSH keys and it's default terminal on Windows will work.

  1. Function I want to test Since I cannot share an entire repository, here is a NodeJS function I want to test
import { execSync } from 'child_process'

/**
 * This function counts the number of updates behind the remote repository.
 *
 * @param repoPath - The path to the git repository
 * @returns Returns the number of updates behind the remote repository or throws
 * an error.
 */
export const gitCountUpdatesBehind = (repoPath: string, branch?: string) => {
  try {
    if (!branch) { branch = 'master' }

    // Fetch the latest data from the remote repository
    execSync(`git -C ${repoPath} fetch origin ${branch}`, { stdio: 'pipe' })

    // Compare the local branch with the remote branch
    const command = `git -C ${repoPath} rev-list origin/${branch}..remotes/origin/${branch} --count`
    const diff = execSync(command, { stdio: 'pipe' }).toString().trim()

    return Number(diff)
  } catch (error: any) {
    throw new Error(`Failed to check for updates on branch "${branch}"!\n\n${error.stderr}`)
  }
}
  1. Test
    
    import { describe, expect, it } from 'vitest'
    import { gitCountUpdatesBehind } from '../git-count-updates-behind.js'

describe('Expect gitCountUpdatesBehind() function', () => { it('to return the number of updates behind on a master branch', () => { const result = gitCountUpdatesBehind('.', 'master')

expect(typeof result).toBe('number')

}) })


### Output

```shell
Failed to check for updates on branch "master"!

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.


### Version

0.10.7

### Validations

- [X] Check that you are using the latest version of the extension
- [X] Check that there isn't [already an issue](https://github.com/vitest-dev/vscode/issues) that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitest-dev/vscode/discussions) or join our [Discord Chat Server](https://chat.vitest.dev).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.