Skip to content

Commit

Permalink
[FIX] migration tests: handle detached head state of repo to test mig…
Browse files Browse the repository at this point in the history
…ration for
  • Loading branch information
katyukha committed Apr 3, 2023
1 parent 5bc8c72 commit 25532ee
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
33 changes: 27 additions & 6 deletions subpackages/lib/source/odood/lib/addons/repository.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ private import std.regex;
private import std.exception: enforce;
private import std.format: format;
private import std.logger;
private import std.typecons: Nullable, nullable;
private static import std.process;

private import thepath: Path;

private import odood.lib.utils: runCmdE;
private import odood.lib.utils: runCmd, runCmdE;
private import odood.lib.project: Project;
private import odood.lib.exception: OdoodException;
private import odood.lib.git: parseGitURL, gitClone;
Expand Down Expand Up @@ -48,18 +49,38 @@ class AddonRepository {
}
}

/** Find the name of current git branch for this repo
/** Find the name of current git branch for this repo.
*
* Returns: Nullable!string
* If current branch is detected, result is non-null.
* If result is null, then git repository is in detached-head mode.
**/
string getCurrBranch() {
Nullable!string getCurrBranch() {
import std.string: chompPrefix, strip;
string git_ref = runCmdE(
auto result = runCmd(
["git", "symbolic-ref", "-q", "HEAD"],
_path,
null,
std.process.Config.stderrPassThrough,
).output;
);
if (result.status == 0)
return result.output.strip().chompPrefix("refs/heads/").nullable;
return Nullable!(string).init;
}

return git_ref.strip().chompPrefix("refs/heads/");
/** Get current commit
*
* Returns:
* SHA1 hash of current commit
**/
string getCurrCommit() {
import std.string: strip;
return runCmdE(
["git", "rev-parse", "-q", "HEAD"],
_path,
null,
std.process.Config.stderrPassThrough,
).output.strip();
}

/** Fetch remote 'origin'
Expand Down
18 changes: 11 additions & 7 deletions subpackages/lib/source/odood/lib/odoo/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,13 @@ struct OdooTestRunner {
"Migration test requested, but migration repo is not specified!");

// Switch branch to migration start ref
string current_branch;
string current_git_ref;
if (_test_migration) {
current_branch = _test_migration_repo.getCurrBranch();
// Get current branch, and if repo is in detached head mode,
// then save current commit, to return to after migration.
current_git_ref = _test_migration_repo.getCurrBranch.get(
_test_migration_repo.getCurrCommit);

if (_test_migration_start_ref) {
infof(
"Switching to %s ref before running migration tests...",
Expand All @@ -372,9 +376,9 @@ struct OdooTestRunner {
}
scope(exit) {
// Ensure that on exit repo will be returned in it's correct state
if (_test_migration && _test_migration_repo.getCurrBranch() != current_branch) {
infof("Switching back to %s ...", current_branch);
_test_migration_repo.switchBranchTo(current_branch);
if (_test_migration && _test_migration_repo.getCurrBranch() != current_git_ref) {
infof("Switching back to %s ...", current_git_ref);
_test_migration_repo.switchBranchTo(current_git_ref);
}
}

Expand Down Expand Up @@ -432,8 +436,8 @@ struct OdooTestRunner {
}

if (_test_migration) {
infof("Switching back to %s ...", current_branch);
_test_migration_repo.switchBranchTo(current_branch);
infof("Switching back to %s ...", current_git_ref);
_test_migration_repo.switchBranchTo(current_git_ref);

// TODO: clean -fdx ?

Expand Down

0 comments on commit 25532ee

Please sign in to comment.