UNPKG

3.58 kBJavaScriptView Raw
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18'use strict';
19
20var assert = require('assert'),
21 fs = require('fs'),
22 path = require('path');
23
24var promise = require('../').promise,
25 io = require('../io'),
26 cmd = require('../lib/command'),
27 remote = require('../remote');
28
29const {enablePromiseManager} = require('../lib/test/promise');
30
31describe('DriverService', function() {
32 describe('start()', function() {
33 var service;
34
35 beforeEach(function() {
36 service = new remote.DriverService(process.execPath, {
37 port: 1234,
38 args: ['-e', 'process.exit(1)']
39 });
40 });
41
42 afterEach(function() {
43 return service.kill();
44 });
45
46 it('fails if child-process dies', function() {
47 this.timeout(1000);
48 return service.start(500).then(expectFailure, verifyFailure);
49 });
50
51 enablePromiseManager(function() {
52 describe(
53 'failures propagate through control flow if child-process dies',
54 function() {
55 it('', function() {
56 this.timeout(1000);
57
58 return promise.controlFlow().execute(function() {
59 promise.controlFlow().execute(function() {
60 return service.start(500);
61 });
62 }).then(expectFailure, verifyFailure);
63 });
64 });
65 });
66
67 function verifyFailure(e) {
68 assert.ok(!(e instanceof promise.CancellationError));
69 assert.equal('Server terminated early with status 1', e.message);
70 }
71
72 function expectFailure() {
73 throw Error('expected to fail');
74 }
75 });
76});
77
78describe('FileDetector', function() {
79 class ExplodingDriver {
80 schedule() {
81 throw Error('unexpected call');
82 }
83 }
84
85 it('returns the original path if the file does not exist', function() {
86 return io.tmpDir(dir => {
87 let theFile = path.join(dir, 'not-there');
88 return (new remote.FileDetector)
89 .handleFile(new ExplodingDriver, theFile)
90 .then(f => assert.equal(f, theFile));
91 });
92 });
93
94 it('returns the original path if it is a directory', function() {
95 return io.tmpDir(dir => {
96 return (new remote.FileDetector)
97 .handleFile(new ExplodingDriver, dir)
98 .then(f => assert.equal(f, dir));
99 });
100 });
101
102 it('attempts to upload valid files', function() {
103 return io.tmpFile(theFile => {
104 return (new remote.FileDetector)
105 .handleFile(
106 new (class FakeDriver {
107 schedule(command) {
108 assert.equal(command.getName(), cmd.Name.UPLOAD_FILE);
109 assert.equal(typeof command.getParameters()['file'], 'string');
110 return Promise.resolve('success!');
111 }
112 }),
113 theFile)
114 .then(f => assert.equal(f, 'success!'));
115 });
116 });
117});