3

related: https://github.com/fish-shell/fish-shell/issues/5444


I'm in the process of testing pure theme for fish.

I want to test the following conditional branch from our __pure_prompt_first_line.fish_:

if not type -fq git
    return 1
end

But I need a way to fake git is missing.

How could I achieve this as I have git install?

2 Answers 2

2

Copying from the linked issue, you could override type to always return 1 when asked about git.

functions --copy type builtin_type
function type  # mock
    if test "x$argv" = "x-fq git"
        return 1
    end
    builtin_type $argv # call original
end

This should work across a variety of environments, and not depend on where git is installed, nor require superuser authority. It's also much more mock-like.

2
  • The first proposition sounds like a nuclear approach which can have a side-effect. I prefer the second one, could you split the answer? Commented Dec 30, 2018 at 17:11
  • 1
    Done; I feel a bit weird taking credit for someone else's answer, though, so if faho has an sqa stack exchange account, I'll be happy to delete this and let him take credit. (His answer is what I'd have proposed if I knew more shell scripting than I do; as you say, updating $PATH is a nuclear option, but if done properly in the context of unit testing, shouldn't harm anything, but is much more complicated..) Commented Dec 30, 2018 at 17:54
1

You could update the $PATH so that git isn't in it, and then restore it afterwards.

However, this will be risky, as it means you'd either need to assume that git is installed somewhere, and just remove that from the path, or determine programmatically where it's located, and remove the path. And then you'd have to worry about it being installed in multiple places, so this would be a fairly complicated solution if you wanted it to be able to run anywhere. (Note that the same set of issues would apply if you wanted to temporarily rename git to something else, plus you'd have to worry about permissions issues and the like.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.