Skip to content

Commit

Permalink
Implement first version of task to switch Xcode versions (#1)
Browse files Browse the repository at this point in the history
* Implement version 1.0
  • Loading branch information
maxim-lobanov authored May 1, 2020
1 parent 9e20a6c commit 16d3b7b
Show file tree
Hide file tree
Showing 16 changed files with 8,629 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"env": {
"node": true,
"es6": true,
"jest/globals": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:jest/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.eslint.json",
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": ["@typescript-eslint", "jest"],
"ignorePatterns": ["node_modules/"],
"rules": {
"indent": ["error", 4],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "double"],
"semi": ["error", "always"]
}
}
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Validate 'setup-xcode'
on:
pull_request:
schedule:
- cron: 0 0 * * *

jobs:
xcode-versions:
name: xcode versions
runs-on: macos-latest
strategy:
matrix:
xcode-version: [10.3, 11, 11.2, 11.4.0, 11.4.1, ^11.4.0, latest]
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v2

- name: setup-xcode
uses: ./
with:
xcode-version: ${{ matrix.xcode-version }}
26 changes: 26 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Build task
on: [pull_request]

jobs:
Build:
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@master

- name: Set Node.JS
uses: actions/setup-node@master
with:
node-version: 12.x

- name: npm install
run: npm install

- name: Build
run: npm run build

- name: Run tests
run: npm run test

- name: Lint
run: npm run lint
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
lib
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

The MIT License (MIT)

Copyright (c) 2020 Maxim Lobanov and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
# setup-xcode
Set up your GitHub Actions workflow with a specific version of Xcode
This action is intended to switch between pre-installed versions of Xcode for macOS images in GitHub Actions.

The list of all available versions can be found in [virtual-environments](https://github.com/actions/virtual-environments/blob/master/images/macos/macos-10.15-Readme.md#xcode) repository.

# Available parameters
| Argument | Description | Format |
|-------------------------|--------------------------|--------------------|
| `xcode-version` | Specify the Xcode version to use | `latest` keyword or any [semver](https://semver.org/) string |
**Examples:** `latest`, `10`, `11.4`, `11.4.0`, `^11.4.0`

# Usage
```
name: CI
on: [push]
jobs:
build:
name: Set
runs-on: macos-latest
steps:
- name: setup-xcode
uses: maxim-lobanov/[email protected]
with:
xcode-version: 11.4 # set the latest available Xcode 11.4.*
- name: setup-latest-xcode
uses: maxim-lobanov/[email protected]
with:
xcode-version: latest # set the latest available Xcode 11.4.*
```

# License
The scripts and documentation in this project are released under the [MIT License](LICENSE)
133 changes: 133 additions & 0 deletions __tests__/xcode-selector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import * as fs from "fs";
import * as child from "child_process";
import * as core from "@actions/core";
import { XcodeSelector, XcodeVersion } from "../src/xcode-selector";

jest.mock("fs");
jest.mock("child_process");
jest.mock("@actions/core");

const buildFsDirentItem = (name: string, opt: { isSymbolicLink: boolean; isDirectory: boolean }): fs.Dirent => {
return {
name,
isSymbolicLink: () => opt.isSymbolicLink,
isDirectory: () => opt.isDirectory
} as fs.Dirent;
};

const fakeReadDirResults = [
buildFsDirentItem("Xcode.app", { isSymbolicLink: true, isDirectory: false }),
buildFsDirentItem("Xcode.app", { isSymbolicLink: false, isDirectory: true }),
buildFsDirentItem("Xcode_11.1.app", { isSymbolicLink: false, isDirectory: true }),
buildFsDirentItem("Xcode_11.1_beta.app", { isSymbolicLink: true, isDirectory: false }),
buildFsDirentItem("Xcode_11.2.1.app", { isSymbolicLink: false, isDirectory: true }),
buildFsDirentItem("Xcode_11.4.app", { isSymbolicLink: true, isDirectory: false }),
buildFsDirentItem("Xcode_11.4_beta.app", { isSymbolicLink: false, isDirectory: true }),
buildFsDirentItem("Xcode_11.app", { isSymbolicLink: false, isDirectory: true }),
buildFsDirentItem("third_party_folder", { isSymbolicLink: false, isDirectory: true }),
];

const fakeGetVersionsResult: XcodeVersion[] = [
{ version: "11.4.0", path: "" },
{ version: "11.2.1", path: "" },
{ version: "11.2.0", path: "" },
{ version: "11.0.0", path: "" },
{ version: "10.3.0", path: "" }
];

describe("XcodeSelector", () => {
describe("getXcodeVersionFromAppPath", () => {
it.each([
["/temp/Xcode_11.app", { version: "11.0.0", path: "/temp/Xcode_11.app"}],
["/temp/Xcode_11.2.app", { version: "11.2.0", path: "/temp/Xcode_11.2.app"}],
["/temp/Xcode_11.2.1.app", { version: "11.2.1", path: "/temp/Xcode_11.2.1.app"}],
["/temp/Xcode_11.2.1_beta.app", { version: "11.2.1", path: "/temp/Xcode_11.2.1_beta.app"}],
["/temp/Xcode.app", null],
["/temp/Xcode_11.2", null],
["/temp/Xcode.11.2.app", null]
])("'%s' -> '%s'", (input: string, expected: XcodeVersion | null) => {
// test private method
const actual = new XcodeSelector()["getXcodeVersionFromAppPath"](input);
expect(actual).toEqual(expected);
});

});

describe("getAllVersions", () => {
beforeEach(() => {
jest.spyOn(fs, "readdirSync").mockImplementation(() => fakeReadDirResults);
});

afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});

it("versions are filtered correctly", () => {
const sel = new XcodeSelector();
const expectedVersions: XcodeVersion[] = [
{ version: "11.4.0", path: "/Applications/Xcode_11.4_beta.app" },
{ version: "11.2.1", path: "/Applications/Xcode_11.2.1.app" },
{ version: "11.1.0", path: "/Applications/Xcode_11.1.app" },
{ version: "11.0.0", path: "/Applications/Xcode_11.app" }
];
expect(sel.getAllVersions()).toEqual(expectedVersions);
});
});

describe("findVersion", () => {
it.each([
["latest", "11.4.0"],
["11", "11.4.0"],
["11.x", "11.4.0"],
["11.2.x", "11.2.1"],
["11.2.0", "11.2.0"],
["10.x", "10.3.0"],
["~11.2.0", "11.2.1"],
["^11.2.0", "11.4.0"],
["< 11.0", "10.3.0"],
["10.0.0 - 11.2.0", "11.2.0"],
["give me latest version", null]
] as [string, string | null][])("'%s' -> '%s'", (versionSpec: string, expected: string | null) => {
const sel = new XcodeSelector();
sel.getAllVersions = (): XcodeVersion[] => fakeGetVersionsResult;
const matchedVersion = sel.findVersion(versionSpec)?.version ?? null;
expect(matchedVersion).toBe(expected);
});
});

describe("setVersion", () => {
let coreExportVariableSpy: jest.SpyInstance;
let fsExistsSpy: jest.SpyInstance;
let fsSpawnSpy: jest.SpyInstance;
const xcodeVersion: XcodeVersion = {
version: "11.4",
path: "/Applications/Xcode_11.4.app"
};

beforeEach(() => {
coreExportVariableSpy = jest.spyOn(core, "exportVariable");
fsExistsSpy = jest.spyOn(fs, "existsSync");
fsSpawnSpy = jest.spyOn(child, "spawnSync");
});

afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});

it("works correctly", () => {
fsExistsSpy.mockImplementation(() => true);
const sel = new XcodeSelector();
sel.setVersion(xcodeVersion);
expect(fsSpawnSpy).toHaveBeenCalledWith("sudo", ["xcode-select", "-s", "/Applications/Xcode_11.4.app"]);
expect(coreExportVariableSpy).toHaveBeenCalledWith("MD_APPLE_SDK_ROOT", "/Applications/Xcode_11.4.app");
});

it("error is thrown if version doesn't exist", () => {
fsExistsSpy.mockImplementation(() => false);
const sel = new XcodeSelector();
expect(() => sel.setVersion(xcodeVersion)).toThrow();
});
});
});
14 changes: 14 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: 'Setup Xcode'
author: 'Maxim Lobanov'
description: 'Set up your GitHub Actions workflow with a specific version of Xcode'
inputs:
xcode-version:
description: 'Version of Xcode to use'
required: false
default: latest
runs:
using: 'node12'
main: 'dist/index.js'
branding:
icon: 'code'
color: 'blue'
Loading

0 comments on commit 16d3b7b

Please sign in to comment.