"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const sinon_1 = require("sinon");
const fs_1 = __importDefault(require("fs"));
const projectRoot = __importStar(require("./get-project-root"));
const logger_1 = require("./logger");
const errors_1 = require("./errors");
describe('[method] findProjectRoot', () => {
    const stubs = {};
    beforeEach(() => {
        stubs.cwd = sinon_1.stub(process, 'cwd');
        stubs.existSync = sinon_1.stub(fs_1.default, 'existsSync');
        stubs.existSync.returns(false);
        stubs.existSync.withArgs('/users/john/project-1/lerna.json').returns(true);
    });
    afterEach(() => {
        stubs.cwd.restore();
        stubs.existSync.restore();
    });
    it('should find project root at project root', () => {
        stubs.cwd.returns('/users/john/project-1');
        expect(projectRoot.findProjectRoot()).toBe('/users/john/project-1');
    });
    it('should find project root in a nested directory', () => {
        stubs.cwd.returns('/users/john/project-1/packages/utils/src');
        expect(projectRoot.findProjectRoot()).toBe('/users/john/project-1');
    });
    it('should throw if not a microlambda project', () => {
        stubs.cwd.returns('/users/john/project-2');
        try {
            projectRoot.findProjectRoot();
        }
        catch (e) {
            expect(e.code).toBe(errors_1.MilaErrorCode.NOT_IN_A_VALID_LERNA_PROJECT);
        }
    });
});
describe('[method] getProjectRoot', () => {
    let findProjectRoot;
    let exit;
    let consoleError;
    beforeEach(() => {
        findProjectRoot = sinon_1.stub(projectRoot, 'findProjectRoot');
        exit = sinon_1.stub(process, 'exit');
        consoleError = sinon_1.stub(console, 'error');
    });
    afterEach(() => {
        findProjectRoot.restore();
        exit.restore();
        consoleError.restore();
    });
    it('should find project', () => {
        findProjectRoot.returns('/users/john/project-1');
        expect(projectRoot.getProjectRoot(new logger_1.Logger())).toBe('/users/john/project-1');
    });
    it('should exit 1 and print error message if not in a valid mila project', () => {
        const e = new errors_1.MilaError(errors_1.MilaErrorCode.NOT_IN_A_VALID_LERNA_PROJECT);
        findProjectRoot.throws(e);
        projectRoot.getProjectRoot(new logger_1.Logger());
        expect(exit.callCount).toBe(1);
        expect(consoleError.callCount).toBe(1);
        expect(exit.getCalls()[0].args).toEqual([1]);
    });
    it('should exit 1 and print error message if any error occurs while resolving project root', () => {
        findProjectRoot.throws(new Error('fs error'));
        projectRoot.getProjectRoot(new logger_1.Logger());
        expect(exit.callCount).toBe(1);
        expect(consoleError.callCount).toBe(2);
        expect(exit.getCalls()[0].args).toEqual([1]);
    });
});
//# sourceMappingURL=get-project-root.spec.js.map