libcryfs/vendor/gitversion/gitversion-1.9/src/test/main_test.py

238 lines
11 KiB
Python

import unittest
import subprocess
import os
import sys
from gitversionbuilder import main, Version
from gitversionbuilder.utils import ChDir
from test import test_utils
from test.test_utils import GitDir, TempFile
class IntegrationTest(unittest.TestCase, test_utils.CodeAsserts):
def test_call_version(self):
result = subprocess.check_output([sys.executable, "-m", "gitversionbuilder", "--version"], stderr=subprocess.STDOUT).strip().decode()
self.assertEqual(Version.VERSION_STRING, result)
def test_call_help(self):
result = subprocess.check_output([sys.executable, "-m", "gitversionbuilder", "--help"], stderr=subprocess.STDOUT).decode()
self.assertRegexpMatches(result, "usage:")
def test_call_python(self):
with GitDir() as git, TempFile() as out_file:
commit_id = git.create_git_commit()
expected = """
# ---------------------------------------------------
# This file is autogenerated by git-version.
# DO NOT MODIFY!
# ---------------------------------------------------
VERSION_STRING = "dev1+rev%s"
GIT_TAG_NAME = "master"
GIT_COMMITS_SINCE_TAG = 1
GIT_COMMIT_ID = "%s"
MODIFIED_SINCE_COMMIT = False
IS_DEV_VERSION = True
IS_STABLE_VERSION = False
""" % (commit_id[0:7], commit_id[0:7])
with open("/dev/null", 'w') as devnull:
subprocess.check_call([sys.executable, "-m", "gitversionbuilder", "--lang", "python", "--dir", git.dir, out_file],
stdout=devnull)
self.assertCodeEqual(expected, self.read_file(out_file))
def test_call_cpp(self):
with GitDir() as git, TempFile() as out_file:
commit_id = git.create_git_commit()
expected = """
// ---------------------------------------------------
// This file is autogenerated by git-version.
// DO NOT MODIFY!
// ---------------------------------------------------
#pragma once
#ifndef MESSMER_GITVERSION_VERSION_H
#define MESSMER_GITVERSION_VERSION_H
namespace version {
constexpr const char *VERSION_STRING = "dev1+rev%s";
constexpr const char *GIT_TAG_NAME = "master";
constexpr const unsigned int GIT_COMMITS_SINCE_TAG = 1;
constexpr const char *GIT_COMMIT_ID = "%s";
constexpr bool MODIFIED_SINCE_COMMIT = false;
constexpr bool IS_DEV_VERSION = true;
constexpr bool IS_STABLE_VERSION = false;
}
#endif
""" % (commit_id[0:7], commit_id[0:7])
with open("/dev/null", 'w') as devnull:
subprocess.check_call([sys.executable, "-m", "gitversionbuilder", "--lang", "cpp", "--dir", git.dir, out_file],
stdout=devnull)
self.assertCodeEqual(expected, self.read_file(out_file))
def test_call_without_specifying_dir(self):
with GitDir() as git, TempFile() as out_file:
commit_id = git.create_git_commit()
expected = """
# ---------------------------------------------------
# This file is autogenerated by git-version.
# DO NOT MODIFY!
# ---------------------------------------------------
VERSION_STRING = "dev1+rev%s"
GIT_TAG_NAME = "master"
GIT_COMMITS_SINCE_TAG = 1
GIT_COMMIT_ID = "%s"
MODIFIED_SINCE_COMMIT = False
IS_DEV_VERSION = True
IS_STABLE_VERSION = False
""" % (commit_id[0:7], commit_id[0:7])
script_dir = os.getcwd()
with ChDir(git.dir), open("/dev/null", 'w') as devnull:
subprocess.check_call([sys.executable, "-m", "gitversionbuilder", "--lang", "python", out_file],
stdout=devnull, env={"PYTHONPATH": script_dir})
self.assertCodeEqual(expected, self.read_file(out_file))
class MainTest(unittest.TestCase, test_utils.CodeAsserts):
def test_empty_git_python(self):
with GitDir() as git, TempFile() as out_file:
commit_id = git.create_git_commit()
expected = """
# ---------------------------------------------------
# This file is autogenerated by git-version.
# DO NOT MODIFY!
# ---------------------------------------------------
VERSION_STRING = "dev1+rev%s"
GIT_TAG_NAME = "master"
GIT_COMMITS_SINCE_TAG = 1
GIT_COMMIT_ID = "%s"
MODIFIED_SINCE_COMMIT = False
IS_DEV_VERSION = True
IS_STABLE_VERSION = False
""" % (commit_id[0:7], commit_id[0:7])
main.create_version_file(git_directory=git.dir, output_file=out_file, lang="python")
self.assertCodeEqual(expected, self.read_file(out_file))
def test_empty_git_cpp(self):
with GitDir() as git, TempFile() as out_file:
commit_id = git.create_git_commit()
expected = """
// ---------------------------------------------------
// This file is autogenerated by git-version.
// DO NOT MODIFY!
// ---------------------------------------------------
#pragma once
#ifndef MESSMER_GITVERSION_VERSION_H
#define MESSMER_GITVERSION_VERSION_H
namespace version {
constexpr const char *VERSION_STRING = "dev1+rev%s";
constexpr const char *GIT_TAG_NAME = "master";
constexpr const unsigned int GIT_COMMITS_SINCE_TAG = 1;
constexpr const char *GIT_COMMIT_ID = "%s";
constexpr bool MODIFIED_SINCE_COMMIT = false;
constexpr bool IS_DEV_VERSION = true;
constexpr bool IS_STABLE_VERSION = false;
}
#endif
""" % (commit_id[0:7], commit_id[0:7])
main.create_version_file(git_directory=git.dir, output_file=out_file, lang="cpp")
self.assertCodeEqual(expected, self.read_file(out_file))
def test_tagged_git(self):
with GitDir() as git, TempFile() as out_file:
commit_id = git.create_git_commit()
git.create_git_tag("1.0.1")
expected = """
# ---------------------------------------------------
# This file is autogenerated by git-version.
# DO NOT MODIFY!
# ---------------------------------------------------
VERSION_STRING = "1.0.1"
GIT_TAG_NAME = "1.0.1"
GIT_COMMITS_SINCE_TAG = 0
GIT_COMMIT_ID = "%s"
MODIFIED_SINCE_COMMIT = False
IS_DEV_VERSION = False
IS_STABLE_VERSION = True
VERSION_COMPONENTS = ["1", "0", "1"]
VERSION_TAG = ""
""" % commit_id[0:7]
main.create_version_file(git_directory=git.dir, output_file=out_file, lang="python")
self.assertCodeEqual(expected, self.read_file(out_file))
def test_tagged_git_with_commits_after_tag(self):
with GitDir() as git, TempFile() as out_file:
git.create_git_commit()
git.create_git_tag("1.0.1")
commit_id = git.create_git_commit()
expected = """
# ---------------------------------------------------
# This file is autogenerated by git-version.
# DO NOT MODIFY!
# ---------------------------------------------------
VERSION_STRING = "1.0.1.dev1+rev%s"
GIT_TAG_NAME = "1.0.1"
GIT_COMMITS_SINCE_TAG = 1
GIT_COMMIT_ID = "%s"
MODIFIED_SINCE_COMMIT = False
IS_DEV_VERSION = True
IS_STABLE_VERSION = False
VERSION_COMPONENTS = ["1", "0", "1"]
VERSION_TAG = ""
""" % (commit_id[0:7], commit_id[0:7])
main.create_version_file(git_directory=git.dir, output_file=out_file, lang="python")
self.assertCodeEqual(expected, self.read_file(out_file))
def test_tagged_git_with_different_tagname_scheme(self):
with GitDir() as git, TempFile() as out_file:
commit_id = git.create_git_commit()
git.create_git_tag("versionone")
expected = """
# ---------------------------------------------------
# This file is autogenerated by git-version.
# DO NOT MODIFY!
# ---------------------------------------------------
VERSION_STRING = "versionone"
GIT_TAG_NAME = "versionone"
GIT_COMMITS_SINCE_TAG = 0
GIT_COMMIT_ID = "%s"
MODIFIED_SINCE_COMMIT = False
IS_DEV_VERSION = False
""" % commit_id[0:7]
main.create_version_file(git_directory=git.dir, output_file=out_file, lang="python")
self.assertCodeEqual(expected, self.read_file(out_file))
def test_tagged_git_with_different_tagname_scheme_modified(self):
with GitDir() as git, TempFile() as out_file:
commit_id = git.create_git_commit()
git.create_git_tag("versionone")
git.add_untracked_file()
expected = """
# ---------------------------------------------------
# This file is autogenerated by git-version.
# DO NOT MODIFY!
# ---------------------------------------------------
VERSION_STRING = "versionone-modified"
GIT_TAG_NAME = "versionone"
GIT_COMMITS_SINCE_TAG = 0
GIT_COMMIT_ID = "%s"
MODIFIED_SINCE_COMMIT = True
IS_DEV_VERSION = True
""" % commit_id[0:7]
main.create_version_file(git_directory=git.dir, output_file=out_file, lang="python")
self.assertCodeEqual(expected, self.read_file(out_file))
if __name__ == '__main__':
unittest.main()