InstantRunVerifierTransform extends Transform { @Override public void transform(@NonNull TransformInvocation invocation) throws IOException, TransformException, InterruptedException { if (invocation.getReferencedInputs().isEmpty()) { throw new RuntimeException("Empty list of referenced inputs"); } try { variantScope.getInstantRunBuildContext().startRecording( InstantRunBuildContext.TaskType.VERIFIER); doTransform(invocation.getReferencedInputs(), invocation.isIncremental()); } finally { variantScope.getInstantRunBuildContext().stopRecording( InstantRunBuildContext.TaskType.VERIFIER); } } InstantRunVerifierTransform(InstantRunVariantScope variantScope); @Override void transform(@NonNull TransformInvocation invocation); @NonNull @Override String getName(); @NonNull @Override Set<QualifiedContent.ContentType> getInputTypes(); @NonNull @Override Set<QualifiedContent.ContentType> getOutputTypes(); @NonNull @Override Set<QualifiedContent.Scope> getScopes(); @NonNull @Override Set<QualifiedContent.Scope> getReferencedScopes(); @NonNull @Override Collection<File> getSecondaryDirectoryOutputs(); @Override boolean isIncremental();  }
@Test public void testNonIncrementalModel() throws TransformException, InterruptedException, IOException { InstantRunVerifierTransform transform = getTransform(); final File tmpDir = temporaryFolder.newFolder(); final File inputClass = new File(tmpDir, "com/foo/bar/InputFile.class"); Files.createParentDirs(inputClass); assertTrue(inputClass.createNewFile()); ImmutableList<TransformInput> transformInputs = ImmutableList.<TransformInput>of(new TransformInput() { @NonNull @Override public Collection<JarInput> getJarInputs() { return ImmutableList.of(); } @NonNull @Override public Collection<DirectoryInput> getDirectoryInputs() { return ImmutableList.<DirectoryInput>of(new DirectoryInputForTests() { @NonNull @Override public Map<File, Status> getChangedFiles() { return ImmutableMap.of(inputClass, Status.ADDED); } @NonNull @Override public File getFile() { return tmpDir; } }); } }); transform.transform(new TransformInvocationBuilder(context) .addOutputProvider(transformOutputProvider) .addReferencedInputs(transformInputs) .build()); FileUtils.deletePath(tmpDir); assertThat(recordedVerification).isEmpty(); assertThat(recordedCopies).hasSize(1); assertThat(recordedCopies).containsEntry(inputClass, new File(backupDir, "com/foo/bar/InputFile.class")); }
@Test public void testIncrementalMode_changedAndAdded() throws TransformException, IOException, InterruptedException { InstantRunVerifierTransform transform = getTransform(); final File tmpDir = temporaryFolder.newFolder(); final File addedFile = new File(tmpDir, "com/foo/bar/NewInputFile.class"); Files.createParentDirs(addedFile); assertTrue(addedFile.createNewFile()); final File changedFile = new File(tmpDir, "com/foo/bar/ChangedFile.class"); assertTrue(changedFile.createNewFile()); final File lastIterationChangedFile = new File(backupDir, "com/foo/bar/ChangedFile.class"); Files.createParentDirs(lastIterationChangedFile); assertTrue(lastIterationChangedFile.createNewFile()); ImmutableList<TransformInput> transformInputs = ImmutableList.<TransformInput>of(new TransformInput() { @NonNull @Override public Collection<JarInput> getJarInputs() { return ImmutableList.of(); } @NonNull @Override public Collection<DirectoryInput> getDirectoryInputs() { return ImmutableList.<DirectoryInput>of(new DirectoryInputForTests() { @NonNull @Override public Map<File, Status> getChangedFiles() { return ImmutableMap.<File, Status>builder() .put(addedFile, Status.ADDED) .put(changedFile, Status.CHANGED) .build(); } @NonNull @Override public File getFile() { return tmpDir; } }); } }); transform.transform(new TransformInvocationBuilder(context) .addOutputProvider(transformOutputProvider) .addReferencedInputs(transformInputs) .setIncrementalMode(true) .build()); FileUtils.deletePath(tmpDir); assertThat(recordedVerification).isEmpty(); assertThat(recordedCopies).hasSize(2); assertThat(recordedCopies).containsEntry( changedFile, lastIterationChangedFile); assertThat(recordedCopies).containsEntry(addedFile, new File(backupDir, "com/foo/bar/NewInputFile.class")); }
@Test public void testIncrementalMode_changedAndDeleted() throws TransformException, IOException, InterruptedException { InstantRunVerifierTransform transform = getTransform(); final File tmpDir = temporaryFolder.newFolder(); final File changedFile = new File(tmpDir, "com/foo/bar/ChangedFile.class"); Files.createParentDirs(changedFile); assertTrue(changedFile.createNewFile()); final File lastIterationChangedFile = new File(backupDir, "com/foo/bar/ChangedFile.class"); Files.createParentDirs(lastIterationChangedFile); assertTrue(lastIterationChangedFile.createNewFile()); final File deletedFile = new File(tmpDir, "com/foo/bar/DeletedFile.class"); ImmutableList<TransformInput> transformInputs = ImmutableList.<TransformInput>of(new TransformInput() { @NonNull @Override public Collection<JarInput> getJarInputs() { return ImmutableList.of(); } @NonNull @Override public Collection<DirectoryInput> getDirectoryInputs() { return ImmutableList.<DirectoryInput>of(new DirectoryInputForTests() { @NonNull @Override public Map<File, Status> getChangedFiles() { return ImmutableMap.<File, Status>builder() .put(changedFile, Status.CHANGED) .put(deletedFile, Status.REMOVED) .build(); } @NonNull @Override public File getFile() { return tmpDir; } }); } }); transform.transform(new TransformInvocationBuilder(context) .addOutputProvider(transformOutputProvider) .addReferencedInputs(transformInputs) .setIncrementalMode(true) .build()); FileUtils.deletePath(tmpDir); assertThat(recordedVerification).hasSize(1); assertThat(recordedVerification).containsEntry( lastIterationChangedFile, changedFile); assertThat(recordedCopies).hasSize(1); assertThat(recordedCopies).containsEntry( changedFile, lastIterationChangedFile); }
@Test public void testSeveralChangedFilesInIncrementalMode() throws IOException, TransformException, InterruptedException { InstantRunVerifierTransform transform = getTransform(); final File tmpDir = temporaryFolder.newFolder(); final File[] files = new File[5]; final File[] lastIterationFiles = new File[5]; for (int i = 0; i < 5; i++) { files[i] = new File(tmpDir, "com/foo/bar/NewInputFile-" + i + ".class"); Files.createParentDirs(files[i]); assertTrue(files[i].createNewFile()); lastIterationFiles[i] = new File(backupDir, "com/foo/bar/NewInputFile-" + i + ".class"); Files.createParentDirs(lastIterationFiles[i]); assertTrue(lastIterationFiles[i].createNewFile()); } ImmutableList<TransformInput> transformInputs = ImmutableList.<TransformInput>of(new TransformInput() { @NonNull @Override public Collection<JarInput> getJarInputs() { return ImmutableList.of(); } @NonNull @Override public Collection<DirectoryInput> getDirectoryInputs() { return ImmutableList.<DirectoryInput>of(new DirectoryInputForTests() { @NonNull @Override public Map<File, Status> getChangedFiles() { ImmutableMap.Builder<File, Status> builder = ImmutableMap.builder(); for (int i=0; i<5; i++) { builder.put(files[i], Status.CHANGED); } return builder.build(); } @NonNull @Override public File getFile() { return tmpDir; } }); } }); transform.transform(new TransformInvocationBuilder(context) .addOutputProvider(transformOutputProvider) .addReferencedInputs(transformInputs) .setIncrementalMode(true) .build()); FileUtils.deletePath(tmpDir); assertThat(recordedVerification).hasSize(5); for (int i=0; i<5; i++) { assertThat(recordedVerification).containsEntry(lastIterationFiles[i], files[i]); } assertThat(recordedCopies).hasSize(5); for (int i=0; i<5; i++) { assertThat(recordedCopies).containsEntry(files[i], lastIterationFiles[i]); } }
@Test public void testStatusAlreadySet() throws TransformException, InterruptedException, IOException { when(instantRunBuildContext.getVerifierResult()).thenReturn( Optional.of(InstantRunVerifierStatus.DEPENDENCY_CHANGED)); final File inputDir = temporaryFolder.newFolder(); final File inputFile = new File(inputDir, "com/foo/bar/InputFile.class"); ImmutableList<TransformInput> transformInputs = ImmutableList.<TransformInput>of(new TransformInput() { @NonNull @Override public Collection<JarInput> getJarInputs() { return ImmutableList.of(); } @NonNull @Override public Collection<DirectoryInput> getDirectoryInputs() { return ImmutableList.<DirectoryInput>of(new DirectoryInputForTests() { @NonNull @Override public Map<File, Status> getChangedFiles() { return ImmutableMap.of(inputFile, Status.ADDED); } @NonNull @Override public File getFile() { return inputDir; } }); } }); getTransform().transform(new TransformInvocationBuilder(context) .addOutputProvider(transformOutputProvider) .addReferencedInputs(transformInputs) .setIncrementalMode(true) .build()); assertThat(instantRunBuildContext.getVerifierResult().isPresent()).isTrue(); assertThat(instantRunBuildContext.getVerifierResult().get()).isEqualTo( InstantRunVerifierStatus.DEPENDENCY_CHANGED); }