// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.

import {PlistBuddy} from "../../../common/ios/plistBuddy";

import * as Q from "q";
import * as sinon from "sinon";

suite("plistBuddy", function() {
    suite("commonContext", function() {
        test("should attempt to modify, then add, plist properties", function() {
            const plistFileName = "testFile.plist";
            const plistProperty = "myProperty";
            const plistValue = "myValue";

            const mockedExecFunc = sinon.stub();
            mockedExecFunc.onFirstCall().returns({ outcome: Q.reject(new Error("Setting does not exist")) });
            mockedExecFunc.onSecondCall().returns({ outcome: Q.resolve("stdout") });
            mockedExecFunc.throws();

            const mockExec: any = {
                exec: mockedExecFunc
            };
            const plistBuddy = new PlistBuddy({ nodeChildProcess: mockExec });

            return plistBuddy.setPlistProperty(plistFileName, plistProperty, plistValue)
                .then(() => {
                    if (!mockedExecFunc.calledWithMatch(/Set/)) {
                        throw new Error("plistBuddy did not attempt to set first");
                    }
                    if (!mockedExecFunc.calledWithMatch(/Add/)) {
                        throw new Error("plistBuddy did not attempt to add after set failed");
                    }
                    if (mockedExecFunc.callCount > 2) {
                        throw new Error("plistBuddy attempted to execute too often");
                    }
                });
        });
    });
});