| 1 | import v.util.version |
| 2 | import os |
| 3 | |
| 4 | fn test_githash() { |
| 5 | if os.getenv('GITHUB_JOB') == '' { |
| 6 | eprintln('> skipping test, since it needs GITHUB_JOB to be defined (it is flaky on development machines, with changing repos and v compiled with `./v self` from uncommitted changes).') |
| 7 | return |
| 8 | } |
| 9 | if !os.exists(os.join_path(@VMODROOT, '.git')) { |
| 10 | eprintln('> skipping test due to missing V .git directory') |
| 11 | return |
| 12 | } |
| 13 | sha := version.githash(@VMODROOT)! |
| 14 | assert sha == @VCURRENTHASH |
| 15 | |
| 16 | git_proj_path := os.join_path(os.vtmp_dir(), 'test_githash') |
| 17 | defer { |
| 18 | os.rmdir_all(git_proj_path) or {} |
| 19 | } |
| 20 | os.execute_opt('git init ${git_proj_path}')! |
| 21 | os.chdir(git_proj_path)! |
| 22 | if sha_ := version.githash(git_proj_path) { |
| 23 | assert false, 'Should not have found an unknown revision' |
| 24 | } else { |
| 25 | assert err.msg().contains('failed to find revision file'), err.msg() |
| 26 | } |
| 27 | os.execute_opt('git config user.name') or { |
| 28 | os.execute_opt('git config user.email "[email protected]"')! |
| 29 | os.execute_opt('git config user.name "V CI"')! |
| 30 | } |
| 31 | os.write_file('v.mod', '')! |
| 32 | os.execute_opt('git add .')! |
| 33 | os.execute_opt('git commit -m "test1"')! |
| 34 | test_rev := os.execute_opt('git rev-parse --short=7 HEAD')!.output.trim_space() |
| 35 | assert test_rev == version.githash(git_proj_path)! |
| 36 | os.write_file('README.md', '')! |
| 37 | os.execute_opt('git add .')! |
| 38 | os.execute_opt('git commit -m "test2"')! |
| 39 | test_rev2 := os.execute_opt('git rev-parse --short=7 HEAD')!.output.trim_space() |
| 40 | assert test_rev2 != test_rev |
| 41 | assert test_rev2 == version.githash(git_proj_path)! |
| 42 | } |
| 43 | |