Skip to content

Commit

Permalink
test the Git prompt (#122)
Browse files Browse the repository at this point in the history
should
- close #24

## description
this PR
- adds three tests for the three building blocks of the prompt
    - `repo-revision` for `get-revision`
    - `repo-current-action` for `git-action`
    - `build-left-prompt` for `get-left-prompt`
- fixes a bug with `$env.CMD_DURATION` -> when the shell starts its
value is `"0823"`, an easter egg of Nushell 😏
- uses sanitized path in `simplify-path` and sanitize paths in
`get-left-prompt` for Windows
  • Loading branch information
amtoine authored Dec 13, 2023
1 parent dddc16d commit b333280
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 12 deletions.
2 changes: 0 additions & 2 deletions src/nu-git-manager-sugar/git/lib/lib.nu
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use ../../git/lib/style.nu [color]
# │ hash │ fa3c0651 │
# │ type │ detached │
# ╰──────┴──────────╯
# TODO: write a test
export def get-revision [
--short-hash: bool # print the hash of a detached HEAD in short format
]: nothing -> record<name: string, hash: string, type: string> {
Expand Down Expand Up @@ -70,7 +69,6 @@ export def get-revision [
}

# https://stackoverflow.com/questions/59603312/git-how-can-i-easily-tell-if-im-in-the-middle-of-a-rebase
# TODO: write a test
export def git-action []: nothing -> string {
let git_dir = ^git rev-parse --git-dir | path expand

Expand Down
24 changes: 15 additions & 9 deletions src/nu-git-manager-sugar/git/lib/prompt.nu
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
use ../../git/lib/lib.nu [get-revision, git-action]
use ../../git/lib/style.nu [color, simplify-path]

# TODO: write a test
# /!\ the PWD will be sanitized
export def get-left-prompt [duration_threshold: duration]: nothing -> string {
let is_git_repo = not (
do --ignore-errors { ^git rev-parse --is-inside-work-tree } | is-empty
)

# FIXME: use `path sanitize` from `nu-git-manager`
let pwd = pwd | str replace --regex '^.:' '' | str replace --all '\' '/'
let pwd = if $is_git_repo {
let repo_root = (
^git rev-parse --show-toplevel
)
# FIXME: use `path sanitize` from `nu-git-manager`
let repo_root = ^git rev-parse --show-toplevel
| str replace --regex '^.:' ''
| str replace --all '\' '/'
let repo = $repo_root | path basename | color "magenta_bold"
let sub_dir = pwd
| str replace $repo_root ''
| str trim --char (char path_sep)
let sub_dir = $pwd
| str replace $repo_root '' # FIXME: use `path remove-prefix` from `nu-git-manager`
| str trim --char '/' # FIXME: use `path remove-trailing-path-sep` from `nu-git-manager`
| simplify-path

if $sub_dir != "" {
Expand All @@ -24,7 +27,7 @@ export def get-left-prompt [duration_threshold: duration]: nothing -> string {
$repo
}
} else {
pwd | simplify-path | color "green"
$pwd | simplify-path | color "green"
}

let git_branch_segment = if $is_git_repo {
Expand Down Expand Up @@ -69,7 +72,10 @@ export def get-left-prompt [duration_threshold: duration]: nothing -> string {
null
}

let cmd_duration = $env.CMD_DURATION_MS | into int | $in * 1ms
let cmd_duration = match $env.CMD_DURATION_MS? {
"0823" | null => -1ms,
_ => ($env.CMD_DURATION_MS | into int | $in * 1ms),
}
let duration_segment = if $cmd_duration > $duration_threshold {
$cmd_duration | color "light_yellow"
} else {
Expand Down
7 changes: 6 additions & 1 deletion src/nu-git-manager-sugar/git/lib/style.nu
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# /!\ requires the input path to be sanitized /!\
export def simplify-path []: path -> string {
str replace $nu.home-path "~" | str replace --regex '^/' "!/"
let input = $in

# FIXME: use `path sanitize` from `nu-git-manager`
let home = $nu.home-path | str replace --regex '^.:' '' | str replace --all '\' '/'
$input | str replace $home "~" | str replace --regex '^/' "!/"
}

export def color [color]: string -> string {
Expand Down
112 changes: 112 additions & 0 deletions tests/sugar/git.nu
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,115 @@ export def branch-compare [] {

clean $foo
}

export module prompt {
use ../../src/nu-git-manager-sugar/git/lib/lib.nu [get-revision, git-action]
use ../../src/nu-git-manager-sugar/git/lib/prompt.nu [get-left-prompt]
use ../../src/nu-git-manager-sugar/git/lib/style.nu [simplify-path]

def "assert revision" [expected: record] {
let actual = get-revision --short-hash true
assert equal $actual $expected
}

def "assert prompt" [expected: string] {
let actual = get-left-prompt 10hr | ansi strip

let admin_segment = if $nu.os-info.name == "windows" {
"!!"
} else {
null
}

assert equal $actual ($admin_segment | append $expected | compact | str join " ")
}

export def repo-revision [] {
let repo = init-repo-and-cd-into
^git config tag.gpgSign false

assert revision {name: "main", hash: "", type: "branch"}

let hashes = commit c1 c2 c3
assert revision {name: "main", hash: ($hashes | last), type: "branch"}

^git checkout $hashes.1
assert revision {name: null, hash: $hashes.1, type: "detached"}

^git tag foo --annotate --message ""
assert revision {name: "foo", hash: $hashes.1, type: "tag"}

^git checkout main
^git tag bar --annotate --message ""
assert revision {name: "main", hash: ($hashes | last), type: "branch"}

^git checkout bar
assert revision {name: "bar", hash: ($hashes | last), type: "tag"}

clean $repo
}

export def repo-current-action [] {
let repo = init-repo-and-cd-into

assert equal (git-action) null

commit init
assert equal (git-action) null

^git checkout -b some main
"foo" | save --force file.txt
^git add file.txt
commit foo

^git checkout -b other main
"bar" | save --force file.txt
^git add file.txt
commit bar

do --ignore-errors { ^git merge some }
assert equal (git-action | ansi strip) $"MERGING"

^git merge --abort
do --ignore-errors { ^git rebase some }
assert equal (git-action | ansi strip) $"REBASE-i"

^git rebase --abort
do --ignore-errors { ^git cherry-pick some }
assert equal (git-action | ansi strip) $"CHERRY-PICKING"

^git cherry-pick --abort
assert equal (git-action) null

clean $repo
}

export def build-left-prompt [] {
let repo = init-repo-and-cd-into

assert prompt $"($repo | path basename) \(main:\) "

let hash = commit init | get 0
assert prompt $"($repo | path basename) \(main:($hash)\) "

mkdir foo
cd foo
let pwd = $repo | path basename | append foo | str join (char path_sep)
assert prompt $"($pwd) \(main:($hash)\) "

cd ..
^git checkout $hash
assert prompt $"($repo | path basename) \(_:($hash)\) "

cd ..
# FIXME: use `path sanitize` from `nu-git-manager`
let expected_pwd = $repo
| path dirname
| str replace --regex '^.:' ''
| str replace --all '\' '/'
| simplify-path
assert prompt $"($expected_pwd) "

clean $repo
}
}

0 comments on commit b333280

Please sign in to comment.