Skip to content

Commit

Permalink
Add train example, travis, easier testing. (google#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
matanlurey authored Mar 26, 2018
1 parent ea7fb98 commit 946914f
Show file tree
Hide file tree
Showing 19 changed files with 289 additions and 9 deletions.
40 changes: 40 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
language: dart

# We currently are building against the Dart2 SDK.
dart:
- dev

# Gives more resources on Travis (8GB Ram, 2CPUs).
# Do not remove without verifying w/ Travis.
sudo: required

# Only building master means that we don't run two builds for each pull request.
branches:
only: [master]

# Re-use downloaded pub packages everywhere.
cache:
directories:
- $HOME/.pub-cache

# Build stages: https://docs.travis-ci.com/user/build-stages/.
stages:
- presubmit
- building
- testing

jobs:
- stage: presubmit
script: ./tool/travis.sh analyze
env: PKG="example/coffee"
- stage: presubmit
script: ./tool/travis.sh analyze
env: PKG="example/train"
- stage: presubmit
script: ./tool/travis.sh analyze
env: PKG="package/inject"
- stage: presubmit
script: ./tool/travis.sh analyze
env: PKG="package/inject_generator"

# TODO: Add build, test phases.
2 changes: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ analyzer:
exclude:
- "**/*.dart_tool/*"
- "**/build/*"
errors:
uri_does_not_exist: ignore
linter:
rules:
# Errors
Expand Down
4 changes: 1 addition & 3 deletions example/coffee/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ $ dart bin/brew.dart
> system and testing package/infrastructure. For now this is a _workaround_.
```bash
$ pub run build_runner build -o build
$ cd build
$ dart test/coffee_app_test.dart
$ pub run build_runner test
```
10 changes: 10 additions & 0 deletions example/coffee/dart_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# TODO(https://github.com/dart-lang/test/issues/772): Headless chrome timeout.
presets:
travis:
override_platforms:
chrome:
settings:
headless: false

platforms:
- chrome
1 change: 0 additions & 1 deletion example/coffee/lib/coffee_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ library inject.example.coffee;
import 'package:inject/inject.dart';

// This is a compile-time generated file and does not exist in source.
// ignore: uri_does_not_exist
import 'coffee_app.inject.dart' as generated;
import 'src/drip_coffee_module.dart';
import 'src/coffee_maker.dart';
Expand Down
5 changes: 3 additions & 2 deletions example/coffee/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ dependencies:
path: ../../package/inject

dev_dependencies:
build_runner: ^0.7.11
build_test: ^0.10.0
build_runner: ^0.8.0
build_test: ^0.10.1+1
build_web_compilers: ^0.3.4+2
inject_generator:
path: ../../package/inject_generator
test: ^0.12.32+1
1 change: 0 additions & 1 deletion example/coffee/test/coffee_app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:test/test.dart';
import 'package:inject.example.coffee/coffee_app.dart';

// This is a compile-time generated file and does not exist in source.
// ignore: uri_does_not_exist
import 'coffee_app_test.inject.dart' as gen;

List<String> _printLog = <String>[];
Expand Down
34 changes: 34 additions & 0 deletions example/train/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Sample app with independently developed sub-libraries

A common question we receive is how to structure an application that's developed
by multiple independent teams. Typically, in such situation the application is
broken up into several feature tracks, with each team owning one of them. One of
the teams usually owns the infrastructure for the apps, which includes the main
entrypoint that "stitches" the code from multiple feature tracks into the whole
application.

This example emulates a multi-team situation. It is modeled after a train with
a locomotive and two cars - a bike car and a food car. Each of the pieces -
locomotive, bike and food - are individual feature tracks, with locomotive being
the entrypoint that connects the other features (cars) into an app (the train).
There's a service that's shared by all cars - CarMaintenance.

## To run the example

> **NOTE**: Our CLI story is a work-in-progress as work continues on the build
> system and testing package/infrastructure. For now this is a _workaround_.
```bash
$ pub run build_runner build -o build
$ cd build
$ dart bin/brew.dart
```

## To run the tests

> **NOTE**: Our test story is a work-in-progress as work continues on the build
> system and testing package/infrastructure. For now this is a _workaround_.
```bash
$ pub run build_runner test
```
10 changes: 10 additions & 0 deletions example/train/dart_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# TODO(https://github.com/dart-lang/test/issues/772): Headless chrome timeout.
presets:
travis:
override_platforms:
chrome:
settings:
headless: false

platforms:
- chrome
33 changes: 33 additions & 0 deletions example/train/lib/bike.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:inject/inject.dart';

import 'common.dart';

/// Provides the service locator to the bike car feature code.
BikeServiceLocator bikeServices;

/// Declares dependencies used by the bike car.
abstract class BikeServiceLocator {
@provide
BikeRack get bikeRack;
}

/// Declares dependencies needed by the bike car.
@module
class BikeServices {
/// Note the dependency on [CarMaintenance] which this module does not itself
/// provide. This tells `package:inject` to look for it when this module is
/// mixed into an injector. The compiler will _statically_ check that this
/// dependency is satisfied, and issue a warning if it's not.
@provide
BikeRack bikeRack(CarMaintenance cm) => new BikeRack(cm);
}

class BikeRack {
final CarMaintenance maintenance;

BikeRack(this.maintenance);

String pleaseFix() {
return maintenance.pleaseFix();
}
}
13 changes: 13 additions & 0 deletions example/train/lib/common.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:inject/inject.dart';

/// Provides common dependencies.
@module
class CommonServices {
@provide
CarMaintenance maintenance() => new CarMaintenance();
}

/// Fixes train cars of all kinds.
class CarMaintenance {
String pleaseFix() => 'Sure thing!';
}
23 changes: 23 additions & 0 deletions example/train/lib/food.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:inject/inject.dart';

import 'common.dart';

/// Provides service locator for food car feature code.
FoodServiceLocator foodServices;

/// Declares dependencies used by the food car.
abstract class FoodServiceLocator {
@provide
Kitchen get kitchen;
}

/// Declares dependencies needed by the food car.
@module
class FoodServices {
@provide
Kitchen kitchen(CarMaintenance cm) => new Kitchen(cm);
}

class Kitchen {
Kitchen(CarMaintenance cm);
}
30 changes: 30 additions & 0 deletions example/train/lib/locomotive.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'dart:async';

import 'package:inject/inject.dart';

import 'bike.dart';
import 'common.dart';
import 'food.dart';

import 'locomotive.inject.dart' as g;

/// The top level injector that stitches together multiple app features into
/// a complete app.
@Injector(const [BikeServices, FoodServices, CommonServices])
abstract class TrainServices implements BikeServiceLocator, FoodServiceLocator {
static Future<TrainServices> create(
BikeServices bikeModule,
FoodServices foodModule,
CommonServices commonModule,
) async {
var services = await g.TrainServices$Injector.create(
bikeModule,
foodModule,
commonModule,
);

bikeServices = services;
foodServices = services;
return services;
}
}
15 changes: 15 additions & 0 deletions example/train/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'dart:async';

import 'bike.dart';
import 'common.dart';
import 'food.dart';
import 'locomotive.dart';

Future<Null> main() async {
final services = await TrainServices.create(
new BikeServices(),
new FoodServices(),
new CommonServices(),
);
print(services.bikeRack.pleaseFix());
}
14 changes: 14 additions & 0 deletions example/train/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: inject.example.train
description: A more complex example of using package:inject

dependencies:
inject:
path: ../../package/inject

dev_dependencies:
build_runner: ^0.8.0
build_test: ^0.10.1+1
build_web_compilers: ^0.3.4+2
inject_generator:
path: ../../package/inject_generator
test: ^0.12.32+1
20 changes: 20 additions & 0 deletions example/train/test/locomotive_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:test/test.dart';

import 'package:inject.example.train/bike.dart';
import 'package:inject.example.train/common.dart';
import 'package:inject.example.train/food.dart';
import 'package:inject.example.train/locomotive.dart';

void main() {
group('locomotive', () {
test('can instantiate TrainServices', () async {
final services = await TrainServices.create(
new BikeServices(),
new FoodServices(),
new CommonServices(),
);
services.bikeRack;
services.kitchen;
});
});
}
2 changes: 2 additions & 0 deletions package/inject/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# `package:inject`

[![Build Status](https://travis-ci.org/google/inject.dart.svg?branch=master)](https://travis-ci.org/google/inject.dart)

Compile-time dependency injection for Dart and Flutter, similar to [Dagger][].

[Dagger]: https://google.github.io/dagger/
Expand Down
2 changes: 0 additions & 2 deletions package/inject_generator/test/utils.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';

import 'package:build/build.dart';
import 'package:build_test/build_test.dart';
Expand Down
39 changes: 39 additions & 0 deletions tool/travis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
if [ "$#" == "0" ]; then
echo -e '\033[31mAt least one task argument must be provided!\033[0m'
exit 1
fi

EXIT_CODE=0

while (( "$#" )); do
TASK=$1
case $TASK in
dartfmt) echo
echo -e '\033[1mTASK: dartfmt\033[22m'
echo -e 'dartfmt -n --set-exit-if-changed .'
dartfmt -n --set-exit-if-changed . || EXIT_CODE=$?
;;
analyze) echo
echo -e '\033[1mTASK: dartanalyzer\033[22m'
echo -e 'dartanalyzer --fatal-warnings .'
dartanalyzer --fatal-warnings . || EXIT_CODE=$?
;;
dartdevc_build) echo
echo -e '\033[1mTASK: build\033[22m'
echo -e 'pub run build_runner build --fail-on-severe'
pub run build_runner build --fail-on-severe || EXIT_CODE=$?
;;
dartdevc_test) echo
echo -e '\033[1mTASK: dartdevc_test\033[22m'
echo -e 'pub run build_runner test -- -P travis'
pub run build_runner test -- -p chrome || EXIT_CODE=$?
;;
*) echo -e "\033[31mNot expecting TASK '${TASK}'. Error!\033[0m"
EXIT_CODE=1
;;
esac

shift
done

exit $EXIT_CODE

0 comments on commit 946914f

Please sign in to comment.