2020-07-28
study
00
请注意,本文编写于 969 天前,最后修改于 81 天前,其中某些信息可能已经过时。

目录

前言
测试
引入依赖
编写测试task
文档生成
引入依赖
编写task
自动化
结语

简单的实践操作

前言

这两天一直在写算法,但是一个一个测试太麻烦了,并且也没有文档看,简直累人,所以就写了个action让它自动生成并部署到GithubPage上面去,爽了。

测试

引入依赖

groovy
dependencies {
    ......
    testImplementation(
            'org.assertj:assertj-core:3.12.2',
            'org.junit.jupiter:junit-jupiter-api:5.4.2'
    )
    testRuntime('org.junit.jupiter:junit-jupiter-engine:5.4.2')
}

编写测试task

groovy
test {
    useJUnitPlatform()
}

完成!看看效果吧~

test

测试成功~

文档生成

本次使用的是kotlin官方文档生成器dokka,内含有多种生成方案,并且可以集成到Gradle中,爽的。

引入依赖

这里以插件方式引入:

groovy
plugins {
    ......
    id 'org.jetbrains.dokka' version '0.10.1'
}

编写task

groovy
dokka {
    // 样式,具体看官方文档
    outputFormat = 'javadoc'
    outputDirectory = "$buildDir/dokka"
    disableAutoconfiguration = false

    cacheRoot = 'default'

    configuration {
        moduleName = 'doc'

        // Use to include or exclude non public members.
        includeNonPublic = false

        // Do not output deprecated members. Applies globally, can be overridden by packageOptions
        skipDeprecated = false

        // Emit warnings about not documented members. Applies globally, also can be overridden by packageOptions
        reportUndocumented = true

        // Do not create index pages for empty packages
        skipEmptyPackages = true

        // This is a list of platform names that will be shown in the final result. See the "Platforms" section of this readme
        targets = ["JVM"]

        // Platform used for code analysis. See the "Platforms" section of this readme
        platform = "JVM"

        sourceRoot {
            // 源文件目录
            path = "src"
        }

        sourceLink {
            // Unix based directory relative path to the root of the project (where you execute gradle respectively).
            path = "src/main/kotlin" // or simply "./"

            // URL showing where the source code can be accessed through the web browser
            url = "https://github.com/xmmmmmovo/Algorithms4thEditionKotlinSolutions/blob/master/src/main/kotlin" //remove src/main/kotlin if you use "./" above

            // 指定行数前缀,github是#L
            lineSuffix = "#L"
        }

        // 用于链接java官方文档
        jdkVersion = 8

        // Disable linking to online kotlin-stdlib documentation
        noStdlibLink = false

        // Disable linking to online JDK documentation
        noJdkLink = false
    }
}

完成!看看效果~

成功~

自动化

每次都运行脚本太累了,干脆直接用CI自动跑了,这里用的是GithubAction,具体如何使用可以看:如何利用GitHubAction和GithubPage部署React应用 这篇,这里只写出yml文件:

yaml
# workflow name
name: Test Workflow

on:
  push:
    branches:
      - master

jobs:
  test:
    name: Kotlin Tests
    runs-on: ubuntu-latest
    env:
      TZ: Asia/Shanghai

    steps:
      # check it to your workflow can access it
      # from: https://github.com/actions/checkout
      - name: Checkout Repository master branch
        uses: actions/checkout@master

      # 验证wrapper
      - name: validate wrapper
        uses: gradle/wrapper-validation-action@v1

      # 安装jdk
      - name: Set up JDK 8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8

      # 缓存
      - name: Cache Gradle packages
        uses: actions/cache@v2
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
          restore-keys: ${{ runner.os }}-gradle

      # 给权限
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      # 测试程序
      - name: Test with gradle
        run: ./gradlew test
yaml
# workflow name
name: Doc Workflow

on:
  release:
    types: [published]
    branches:
      - master

jobs:
  test:
    name: Kotlin Documentation Generation
    runs-on: ubuntu-latest
    env:
      TZ: Asia/Shanghai

    steps:
      # check it to your workflow can access it
      # from: https://github.com/actions/checkout
      - name: Checkout Repository master branch
        uses: actions/checkout@master

      # 验证wrapper
      - name: validate wrapper
        uses: gradle/wrapper-validation-action@v1

      # 安装jdk
      - name: Set up JDK 8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8

      # 缓存
      - name: Cache Gradle packages
        uses: actions/cache@v2
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
          restore-keys: ${{ runner.os }}-gradle

      # 给权限
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      # 缓存
      # 暂时还未找到hash值的解决方案,找到了请pr
      # - name: Cache Dokka
      #   uses: actions/cache@v2
      #   with:
      #     path: ~/.cache/dokka
      #     key: ${{ runner.os }}-dokka-${{ hashFiles('**/*') }}
      #     restore-keys: ${{ runner.os }}-dokka

      # 生成文档
      - name: Generate documentation
        run: ./gradlew dokka

      # 部署到ghpage
      - name: Deploy to gh-pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
          publish_dir: ./build/dokka

完成!测试一下~

成功~

结语

爽到了爽到了~

GitHub地址

如果对你有用的话,可以打赏哦
打赏
logo ali pay
logo wechat pay

本文作者:xmmmmmovo

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 许可协议。转载请注明出处!