File: VcsRepositoryTest.php

package info (click to toggle)
composer 2.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,528 kB
  • sloc: php: 83,030; makefile: 59; xml: 39
file content (186 lines) | stat: -rw-r--r-- 5,624 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php declare(strict_types=1);

/*
 * This file is part of Composer.
 *
 * (c) Nils Adermann <naderman@naderman.de>
 *     Jordi Boggiano <j.boggiano@seld.be>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Test\Repository;

use Composer\Test\TestCase;
use Composer\Util\Platform;
use Symfony\Component\Process\ExecutableFinder;
use Composer\Package\Dumper\ArrayDumper;
use Composer\Repository\VcsRepository;
use Composer\Util\Filesystem;
use Composer\Util\ProcessExecutor;
use Composer\IO\NullIO;
use Composer\Config;

/**
 * @group slow
 */
class VcsRepositoryTest extends TestCase
{
    /**
     * @var string
     */
    private static $composerHome;
    /**
     * @var string
     */
    private static $gitRepo;
    /**
     * @var ?string
     */
    private $skipped = null;

    protected function initialize(): void
    {
        $locator = new ExecutableFinder();
        if (!$locator->find('git')) {
            $this->skipped = 'This test needs a git binary in the PATH to be able to run';

            return;
        }

        $oldCwd = Platform::getCwd();
        self::$composerHome = self::getUniqueTmpDirectory();
        self::$gitRepo = self::getUniqueTmpDirectory();

        if (!@chdir(self::$gitRepo)) {
            $this->skipped = 'Could not move into the temp git repo '.self::$gitRepo;

            return;
        }

        // init
        $process = new ProcessExecutor;
        $exec = static function ($command) use ($process): void {
            $cwd = Platform::getCwd();
            if ($process->execute($command, $output, $cwd) !== 0) {
                throw new \RuntimeException('Failed to execute '.$command.': '.$process->getErrorOutput());
            }
        };

        $exec('git init -q');
        $exec('git checkout -b master');
        $exec('git config user.email composertest@example.org');
        $exec('git config user.name ComposerTest');
        $exec('git config commit.gpgsign false');
        touch('foo');
        $exec('git add foo');
        $exec('git commit -m init');

        // non-composed tag & branch
        $exec('git tag 0.5.0');
        $exec('git branch oldbranch');

        // add composed tag & master branch
        $composer = ['name' => 'a/b'];
        file_put_contents('composer.json', json_encode($composer));
        $exec('git add composer.json');
        $exec('git commit -m addcomposer');
        $exec('git tag 0.6.0');

        // add feature-a branch
        $exec('git checkout -b feature/a-1.0-B');
        file_put_contents('foo', 'bar feature');
        $exec('git add foo');
        $exec('git commit -m change-a');

        // add foo#bar branch which should result in dev-foo+bar
        $exec('git branch foo#bar');

        // add version to composer.json
        $exec('git checkout master');
        $composer['version'] = '1.0.0';
        file_put_contents('composer.json', json_encode($composer));
        $exec('git add composer.json');
        $exec('git commit -m addversion');

        // create tag with wrong version in it
        $exec('git tag 0.9.0');
        // create tag with correct version in it
        $exec('git tag 1.0.0');

        // add feature-b branch
        $exec('git checkout -b feature-b');
        file_put_contents('foo', 'baz feature');
        $exec('git add foo');
        $exec('git commit -m change-b');

        // add 1.0 branch
        $exec('git checkout master');
        $exec('git branch 1.0');

        // add 1.0.x branch
        $exec('git branch 1.1.x');

        // update master to 2.0
        $composer['version'] = '2.0.0';
        file_put_contents('composer.json', json_encode($composer));
        $exec('git add composer.json');
        $exec('git commit -m bump-version');

        chdir($oldCwd);
    }

    public function setUp(): void
    {
        if (!self::$gitRepo) {
            $this->initialize();
        }
        if ($this->skipped) {
            $this->markTestSkipped($this->skipped);
        }
    }

    public static function tearDownAfterClass(): void
    {
        $fs = new Filesystem;
        $fs->removeDirectory(self::$composerHome);
        $fs->removeDirectory(self::$gitRepo);
    }

    public function testLoadVersions(): void
    {
        $expected = [
            '0.6.0' => true,
            '1.0.0' => true,
            '1.0.x-dev' => true,
            '1.1.x-dev' => true,
            'dev-feature-b' => true,
            'dev-feature/a-1.0-B' => true,
            'dev-foo+bar' => true,
            'dev-master' => true,
            '9999999-dev' => true, // alias of dev-master
        ];

        $config = new Config();
        $config->merge([
            'config' => [
                'home' => self::$composerHome,
            ],
        ]);
        $httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();
        $repo = new VcsRepository(['url' => self::$gitRepo, 'type' => 'vcs'], new NullIO, $config, $httpDownloader);
        $packages = $repo->getPackages();
        $dumper = new ArrayDumper();

        foreach ($packages as $package) {
            if (isset($expected[$package->getPrettyVersion()])) {
                unset($expected[$package->getPrettyVersion()]);
            } else {
                $this->fail('Unexpected version '.$package->getPrettyVersion().' in '.json_encode($dumper->dump($package)));
            }
        }

        self::assertEmpty($expected, 'Missing versions: '.implode(', ', array_keys($expected)));
    }
}