| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0bded48 commit f42361e
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,19 +1,23 @@ | |||
| 1 | + #include <filesystem> | ||
| 1 | 2 | #include "init_subcommand.hpp" | |
| 2 | - | ||
| 3 | - //#include "../wrapper/repository_wrapper.hpp" | ||
| 3 | + #include "../wrapper/repository_wrapper.hpp" | ||
| 4 | 4 | ||
| 5 | 5 | InitSubcommand::InitSubcommand(CLI::App& app) | |
| 6 | 6 | { | |
| 7 | 7 | auto *sub = app.add_subcommand("init", "Explanation of init here"); | |
| 8 | 8 | ||
| 9 | - sub->add_flag("--bare", bare, "--- bare ---"); | ||
| 9 | + sub->add_flag("--bare", bare, "info about bare arg"); | ||
| 10 | + | ||
| 11 | + // If directory not specified, uses cwd. | ||
| 12 | + sub->add_option("directory", directory, "info about directory arg") | ||
| 13 | + ->check(CLI::ExistingDirectory | CLI::NonexistentPath) | ||
| 14 | + ->default_val(std::filesystem::current_path()); | ||
| 10 | 15 | ||
| 11 | 16 | sub->callback([this]() { this->run(); }); | |
| 12 | 17 | } | |
| 13 | 18 | ||
| 14 | 19 | void InitSubcommand::run() | |
| 15 | 20 | { | |
| 16 | - std::cout << "RUN " << bare << std::endl; | ||
| 17 | - //RepositoryWrapper repo; | ||
| 18 | - //repo.init(bare); | ||
| 21 | + RepositoryWrapper repo; | ||
| 22 | + repo.init(directory, bare); | ||
| 19 | 23 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | #pragma once | |
| 2 | 2 | ||
| 3 | + #include <string> | ||
| 3 | 4 | #include "base_subcommand.hpp" | |
| 4 | 5 | ||
| 5 | 6 | class InitSubcommand : public BaseSubcommand | |
@@ -10,4 +11,5 @@ class InitSubcommand : public BaseSubcommand | |||
| 10 | 11 | ||
| 11 | 12 | private: | |
| 12 | 13 | bool bare; | |
| 14 | + std::string directory; | ||
| 13 | 15 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,15 +15,8 @@ RepositoryWrapper::~RepositoryWrapper() | |||
| 15 | 15 | } | |
| 16 | 16 | } | |
| 17 | 17 | ||
| 18 | - void RepositoryWrapper::init(bool bare) | ||
| 18 | + void RepositoryWrapper::init(const std::string& directory, bool bare) | ||
| 19 | 19 | { | |
| 20 | - std::cout << "repo init - start" << std::endl; | ||
| 21 | - | ||
| 22 | - // what if it is already initialised??? | ||
| 23 | - | ||
| 24 | - // convert error code to exception | ||
| 25 | - std::string path = "repo"; | ||
| 26 | - throwIfError(git_repository_init(&_repo, path.c_str(), bare)); | ||
| 27 | - | ||
| 28 | - std::cout << "repo init - end " << std::endl; | ||
| 20 | + // what if it is already initialised? Throw exception or delete and recreate? | ||
| 21 | + throwIfError(git_repository_init(&_repo, directory.c_str(), bare)); | ||
| 29 | 22 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,7 @@ class RepositoryWrapper : public BaseWrapper | |||
| 9 | 9 | ||
| 10 | 10 | virtual ~RepositoryWrapper(); | |
| 11 | 11 | ||
| 12 | - void init(bool bare); | ||
| 12 | + void init(const std::string& directory, bool bare); | ||
| 13 | 13 | ||
| 14 | 14 | private: | |
| 15 | 15 | git_repository *_repo; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,15 +1,19 @@ | |||
| 1 | + import pytest | ||
| 1 | 2 | import subprocess | |
| 2 | 3 | ||
| 3 | - def test_version(): | ||
| 4 | - cmd = ['build/git2cpp', '-v'] | ||
| 4 | + | ||
| 5 | + @pytest.mark.parametrize("arg", ['-v', '--version']) | ||
| 6 | + def test_version(arg): | ||
| 7 | + cmd = ['build/git2cpp', arg] | ||
| 5 | 8 | p = subprocess.run(cmd, capture_output=True) | |
| 6 | 9 | assert p.returncode == 0 | |
| 7 | - assert len(p.stderr) == 0 | ||
| 10 | + assert p.stderr == b'' | ||
| 8 | 11 | assert p.stdout.startswith(b'git2cpp ') | |
| 9 | 12 | ||
| 10 | - def test_unknown_option(): | ||
| 13 | + | ||
| 14 | + def test_error_on_unknown_option(): | ||
| 11 | 15 | cmd = ['build/git2cpp', '--unknown'] | |
| 12 | 16 | p = subprocess.run(cmd, capture_output=True) | |
| 13 | - #assert p.returncode == 1 | ||
| 14 | - assert len(p.stdout) == 0 | ||
| 17 | + assert p.returncode == 1 | ||
| 18 | + assert p.stdout == b'' | ||
| 15 | 19 | assert p.stderr.startswith(b"The following argument was not expected: --unknown") | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,67 @@ | |||
| 1 | + import os | ||
| 2 | + from pathlib import Path | ||
| 3 | + import pytest | ||
| 4 | + import subprocess | ||
| 5 | + | ||
| 6 | + | ||
| 7 | + # Fixture to run test in current tmp_path | ||
| 8 | + @pytest.fixture | ||
| 9 | + def run_in_tmp_path(tmp_path): | ||
| 10 | + original_cwd = os.getcwd() | ||
| 11 | + os.chdir(tmp_path) | ||
| 12 | + yield | ||
| 13 | + os.chdir(original_cwd) | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + def test_init_in_directory(tmp_path): | ||
| 17 | + # tmp_path exists and is empty. | ||
| 18 | + assert list(tmp_path.iterdir()) == [] | ||
| 19 | + | ||
| 20 | + cmd = ['/Users/iant/github/git2cpp/build/git2cpp', 'init', '--bare', str(tmp_path)] | ||
| 21 | + p = subprocess.run(cmd, capture_output=True) | ||
| 22 | + assert p.returncode == 0 | ||
| 23 | + assert p.stdout == b'' | ||
| 24 | + assert p.stderr == b'' | ||
| 25 | + | ||
| 26 | + assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [ | ||
| 27 | + 'HEAD', 'config', 'description', 'hooks', 'info', 'objects', 'refs' | ||
| 28 | + ] | ||
| 29 | + | ||
| 30 | + # TODO: check this is a valid git repo | ||
| 31 | + | ||
| 32 | + | ||
| 33 | + def test_init_in_cwd(tmp_path, run_in_tmp_path): | ||
| 34 | + # tmp_path exists and is empty. | ||
| 35 | + assert list(tmp_path.iterdir()) == [] | ||
| 36 | + assert Path.cwd() == tmp_path | ||
| 37 | + | ||
| 38 | + cmd = ['/Users/iant/github/git2cpp/build/git2cpp', 'init', '--bare'] | ||
| 39 | + p = subprocess.run(cmd, capture_output=True) | ||
| 40 | + assert p.returncode == 0 | ||
| 41 | + assert p.stdout == b'' | ||
| 42 | + assert p.stderr == b'' | ||
| 43 | + | ||
| 44 | + assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [ | ||
| 45 | + 'HEAD', 'config', 'description', 'hooks', 'info', 'objects', 'refs' | ||
| 46 | + ] | ||
| 47 | + | ||
| 48 | + # TODO: check this is a valid git repo | ||
| 49 | + | ||
| 50 | + | ||
| 51 | + # TODO: Test without bare flag. | ||
| 52 | + | ||
| 53 | + | ||
| 54 | + def test_error_on_unknown_option(): | ||
| 55 | + cmd = ['build/git2cpp', 'init', '--unknown'] | ||
| 56 | + p = subprocess.run(cmd, capture_output=True) | ||
| 57 | + assert p.returncode == 1 | ||
| 58 | + assert p.stdout == b'' | ||
| 59 | + assert p.stderr.startswith(b"The following argument was not expected: --unknown") | ||
| 60 | + | ||
| 61 | + | ||
| 62 | + def test_error_on_repeated_directory(): | ||
| 63 | + cmd = ['build/git2cpp', 'init', 'abc', 'def'] | ||
| 64 | + p = subprocess.run(cmd, capture_output=True) | ||
| 65 | + assert p.returncode == 1 | ||
| 66 | + assert p.stdout == b'' | ||
| 67 | + assert p.stderr.startswith(b"The following argument was not expected: def") | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments